Table of Contents
In this article I will share example and sample systemd unit service file to run script with systemd right before login prompt in CentOS/RHEL 7/8 Linux. Using this systemd unit file you can run either some command or script as a last service on boot in Linux.
Some more articles on similar topic:
- How to execute a command or script with systemd at shutdown only and not at reboot in Linux
- How to execute a command or script with systemd right before login prompt appears on terminal in Linux
- How to execute a command or script at system startup using systemd without using cronjob in Linux
- How to execute a command or script after N minutes of boot up (system startup) with systemd in Linux
- How to halt system reboot or shutdown and read user input during boot up stage in Linux
- How to execute a command or script using systemd right before shutdown happens in Linux
- How to run a service as a specific user or group using systemd in Linux
Ideally the last service we expect to be called on boot is the getty process which is responsible for showing the login prompt on the Linux console. So here our requirement is basically to execute script with systemd which should be called before
I will be using CentOS/RHEL 7/8 Linux node to verify the steps from this article to run script with systemd right before login prompt.
Step 1: Overview on systemd
I hope you are already familiar with below topics
- Overview on systemd and how it is different from legacy SysV scripts
- How to create a systemd unit file in Linux
Step 2: Create Sample Script
Now to run script with systemd right before login prompt we need a script or command. For the sake of this article I have create a dummy shell script
/tmp/script.sh which we will use for testing this article. I know the script is very dirty but let's focus on the main agenda of this article as it serves the purpose.
[root@centos-8 ~]# cat /tmp/script.sh #!/bin/bash # Run script with systemd right before login prompt case $1 in start) systemctl list-jobs >> /tmp/file echo "====================" >> /tmp/file systemctl list-jobs | egrep -q 'getty.target.*start' && echo "starting script before login prompt" >> /tmp/file exit 0 ;; esac
- The script once called will list the currently active jobs and targets. So with this command we will know the list of services and targets which are in the pipeline to be started or running.
- So if we see
getty.targetis planned to start then our script will echo "starting script before login prompt" into a temporary file
- We have explicitly not added a stop function as that is not required for this article.
Provide executable permission to the script
[root@centos-8 ~]# chmod u+x /tmp/script.sh
Step 3: Create unit file to run script with systemd right before login prompt
Now as highlighted under step 1, I have already written another article with the steps to create a new systemd unit file. Here we will name our systemd unit file as
/etc/systemd/system. Below is the content of
[root@centos-8 ~]# cat /etc/systemd/system/run-before-login-prompt.service [Unit] Description=Run script with systemd right before login prompt After=systemd-user-sessions.service plymouth-quit-wait.service After=rc-local.service Before=getty.target [Service] Type=oneshot RemainAfterExit=yes ExecStart=/tmp/script.sh start [Install] WantedBy=multi-user.target
Here the main task is done by
After= directives from systemd.unit
Before=, After= These two settings expect a space-separated list of unit names. They configure ordering dependencies between units. If a unit foo.service contains a setting Before=bar.service and both units are being started, bar.service's start-up is delayed until foo.service has finished starting up.
Refresh the systemd configuration files
[root@centos-8 ~]# systemctl daemon-reload
Enable the service to automatically start at next boot
[root@centos-8 system]# systemctl enable run-before-login-prompt.service Created symlink /etc/systemd/system/multi-user.target.wants/run-before-login-prompt.service → /etc/systemd/system/run-before-login-prompt.service.
Step 4: Verify the systemd unit file configuration
Now since we are done with the setting up of systemd. Let us verify our configuration. We will reboot our node and check the content of
/tmp/file post reboot
[root@centos-8 ~]# reboot login as: root email@example.com's password: Last login: Thu Jan 16 11:01:16 2020 from 10.0.2.2
Next verify the content of /tmp/file
[root@centos-8 ~]# cat /tmp/file JOB UNIT TYPE STATE 259 tuned.service start running 232 NetworkManager-wait-online.service start running 236 vdo.service start running 231 network-online.target start waiting 129 multi-user.target start waiting 243 libvirtd.service start waiting 230 rpc-statd-notify.service start waiting 220 systemd-update-utmp-runlevel.service start waiting 265 getty.target start waiting 270 systemd-logind.service start running 218 run-before-login-prompt.service start running 217 kdump.service start waiting 279 rsyslog.service start waiting 274 filebeat.service start waiting 14 jobs listed. ==================== starting script before login prompt
So as expected we were able to run script with systemd right before login prompt. At this moment there were 14 jobs planned to start wherein couple of services were in running state including
run-before-login-prompt.service and others. Since we had added a dependency of starting before
getty.target our script was called before login prompt appeared (i.e. before
getty.target entered running state)
systemd-analyze plot > file.svgto generate an image of the boot process for inspection. You can use any browser to view this
file.svgand verify the boot process. There could be one or two short-lived services starting after
run-before-login-prompt.service. If that's a problem, modify
Lastly I hope the steps from the article to run script with systemd right before login prompt at boot up time on CentOS/RHEL 7/8 Linux was helpful. So, let me know your suggestions and feedback using the comment section.
Related Searches: How to execute script right before login prompt in CentOS/RHEL 7/8 Linux. How to execute a command or script before Linux boots up completely after reboot.