How to run script with systemd right before login prompt in CentOS/RHEL 7/8

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:

 

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 getty process.

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

 

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.target is planned to start then our script will echo "starting script before login prompt" into a temporary file /tmp/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 run-before-login-prompt.service under /etc/systemd/system. Below is the content of run-before-login-prompt.service

[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 Before= and 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
root@127.0.0.1'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)

NOTE:

After boot, use systemd-analyze plot > file.svg to generate an image of the boot process for inspection. You can use any browser to view this file.svg and 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 /etc/systemd/system/run-before-login-prompt.service to set Type=idle

 

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.

Leave a Comment

Please use shortcodes <pre class=comments>your code</pre> for syntax highlighting when adding code.