How to run script on startup using systemd in Linux


systemd, How To

In this article I will share a sample systemd unit file which you can use to run script at startup with systemd without using crontab in Linux.

There can be various scenarios when you expect a script or command to be called at startup such as

  • Execute a script after waiting for N minutes of startup
  • Execute a script after all the systemd services are loaded
  • Execute a script immediately after login prompt appears
  • Execute a script just before the login prompt appears

In this article I will cover below two topics as they are almost similar

  • Run script at startup with systemd after network is reachable
  • Execute script at starup after all the systemd services are loaded

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

If you are a beginner to systemd then I would recommend you to also read Overview on systemd and how it is different from legacy SysV scripts before starting with this tutorial.

 

Step 2: Create Sample Script

Now to run script at startup with systemd firstly we need a script or command. For the sake of this article I have create a dummy shell script /tmp/startup_script.sh which we will use for testing this article.

[root@centos-8 ~]# cat /tmp/startup_script.sh
#!/bin/bash
SCRIPT_NAME=$(basename -- "$0")

z=0
for i in {1..5}; do
    sleep 1m
    ((z++))
    wall $SCRIPT_NAME: finished minute ${z}
done
wall $SCRIPT_NAME: COMPLETELY FINISHED

This script will continue to run for 5 minutes and will print an echo statement on the screen every minute as a broadcast message using wall command for all Linux users on the respective node. And at the end of 5th minute it will print a completed broadcast. With this we can also make sure that the script is not killed by systemd if it continues to run for 5 minutes.

Provide executable permission to the script

chmod u+x /tmp/startup_script.sh

Create a new systemd service unit file with following content:

[root@centos-8 ~]# cat /etc/systemd/system/run-at-startup.service
[Unit]
Description=Run script at startup after network becomes reachable

[Service]
Type=simple
RemainAfterExit=yes
ExecStart=/tmp/startup_script.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target

Next to activate the changes, execute following command:

systemctl daemon-reload

Enable the service to make sure this is called automatically after reboot

[root@centos-8 ~]# systemctl enable run-at-startup.service
Created symlink /etc/systemd/system/default.target.wants/run-at-startup.service → /etc/systemd/system/run-at-startup.service.

 

Step 3: Create systemd unit file for different scenario

Step 3.1: Run script at startup with systemd after network becomes reachable

In this scenario we will execute our script as soon as the network becomes reachable i.e. when network.target starts running during bootup stage by using After=network.target. The unit file should be located in either /usr/lib/systemd/system or /etc/systemd/system. I will place this under /etc/systemd/system as this is is used to place custom unit file.

[root@centos-8 ~]# cat /etc/systemd/system/run-at-startup.service
[Unit]
Description=Run script at startup after network becomes reachable
After=network.target

[Service]
Type=simple
RemainAfterExit=yes
ExecStart=/tmp/startup_script.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target

 

Step 3.2: Run script at startup with systemd after all systemd services are loaded

Now in this senario we must make sure all the systemd services for the respective target has loaded before starting the script. So to run script at startup with systemd after all systemd services are loaded we must defined our After= directive accordingly. Since the default.target will vary based on environment so rather than specifying specific target name we will use After=default.target so systemd will decide on it's own the default.target and will call the script at startup in Linux.

[root@centos-8 ~]# cat /etc/systemd/system/run-at-startup.service
[Unit]
Description=Run script at startup after all systemd services are loaded
After=default.target

[Service]
Type=simple
RemainAfterExit=yes
ExecStart=/tmp/startup_script.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target

 

Step 3.3: Run script at startup with systemd after login prompt appears

Now in this scenario also we will only play around After= directive of the unit file. Now since the requirement is to run script at startup with systemd after login prompt appears then we will use After=getty.target

[root@centos-8 ~]# cat /etc/systemd/system/run-at-startup.service
[Unit]
Description=Run script at startup after all systemd services are loaded
After=getty.target

[Service]
Type=simple
RemainAfterExit=yes
ExecStart=/tmp/startup_script.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target

Here,

After=        If the script needs any other system facilities (networking, etc), modify the [Unit] section to include appropriate 
		   After=, Wants=, or Requires= directives, as described in: man systemd.unit

Type=              Switch Type=simple for Type=idle in the [Service] section to delay execution of the script until all other 
		   jobs are dispatched (see man systemd.service for even more choices -- e.g., Type=oneshot can be useful in concert with other services).

TimeoutStartSec=   When a service doesn't signal start-up completion within TimeoutStartSec, systemd considers the service failed;
		   for long-running shell scripts it is essential to modify TimeoutStartSec or disable the timeout logic altogether 
		   as above, with TimeoutStartSec=0. See man systemd.service for more details.

Here if you observe we have defined After=network.target to make sure the script

Refresh the systemd configuration files and enable the service

systemctl daemon-reload
systemctl enable run-at-startup.service

 

Step 4: Verify the systemd unit file configuration

Now our configuration is in place to run script at startup in CentOS/RHEL 7/8 Linux.

Now post reboot of the Linux node we can see that our startup_script.sh is running in the background

[root@centos-8 ~]# ps -ef | grep startup
root       805     1  0 11:38 ?        00:00:00 /bin/bash /tmp/startup_script.sh
root      1198  1147  0 11:39 pts/0    00:00:00 grep --color=auto startup

Also in some time we start getting the broadcast messages from root user.

Broadcast message from root@centos-8.example.com (somewhere) (Thu Jan 16 11:39:

startup_script.sh: finished minute 1


Broadcast message from root@centos-8.example.com (somewhere) (Thu Jan 16 11:40:

startup_script.sh: finished minute 2


Broadcast message from root@centos-8.example.com (somewhere) (Thu Jan 16 11:41:

startup_script.sh: finished minute 3


Broadcast message from root@centos-8.example.com (somewhere) (Thu Jan 16 11:42:

startup_script.sh: finished minute 4


Broadcast message from root@centos-8.example.com (somewhere) (Thu Jan 16 11:43:

startup_script.sh: finished minute 5


Broadcast message from root@centos-8.example.com (somewhere) (Thu Jan 16 11:43:

startup_script.sh: COMPLETELY FINISHED

I have verified the script for both scenarios to run script at startup with systemd (witout crontab) in Linux but I am not putting output from both scenarios as the output is same in both case.

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-at-startup.service. If that's a problem, modify /etc/systemd/system/run-at-startup.service to set Type=idle

Lastly I hope the steps from the article to run script at startup with systemd without using crontab in 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 at startup as soon as network is reacable in Linux. How to call a script or execute command at Linux startup once the login prompt is visible in Linux. How to execute script with systemd at startup once all the systemd services are loaded in RHEL/CentOS 7/8 Linux with examples. How to run script at startup with crontab and cron job. how to add startup scripts in redhat linux 7. run script on startup.

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 admin@golinuxcloud.com

Thank You for your support!!

8 thoughts on “How to run script on startup using systemd in Linux”

  1. Got this error . how to fix it?

    [root@galaxy ~]# systemctl enable run-at-startup.service
    Failed to enable unit: Unit file run-at-startup.service does not exist.
    Reply
  2. how would one write a SINGLE service file to run before shutdown, reboot AND also AFTER the network is up on startup? Or cannot this be done for both in a single file?
    thanks
    rich

    Reply
    • Any normal systemd service is expected to run at shutdown if ExecStop is found, similarly ExecStart is required to start a service during reboot and if you want it to happen after network then you can use After=network.target

      Although to run a service before shutdown and cover other scenarios, you can go through different articles which I have published on similar topics.

      Reply
      • OK But can these BOTH be put into a single service file? Or does the shutdown part have to go in one .service file and the startup in a separate .service file?
        thanks
        rich

        Reply

Leave a Comment