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

How To

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/ 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/
# Run script with systemd right before login prompt

case $1 in
        systemctl list-jobs >> /tmp/file
        echo "====================" >> /tmp/file

        systemctl list-jobs | egrep -q '*start' && echo "starting script before login prompt" >> /tmp/file
		exit 0
  • 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 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/


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
Description=Run script with systemd right before login prompt
After=systemd-user-sessions.service plymouth-quit-wait.service

ExecStart=/tmp/ start


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/ → /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@'s password:
Last login: Thu Jan 16 11:01:16 2020 from

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                start waiting
129                    start waiting
243 libvirtd.service                     start waiting
230 rpc-statd-notify.service             start waiting
220 systemd-update-utmp-runlevel.service start waiting
265                         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 our script was called before login prompt appeared (i.e. before entered running state)

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.

Views: 175
Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to

Thank You for your support!!

Leave a Comment