How To Run Script at Startup Using Systemd in Linux - GoLinuxCloud
How To Run Script at Startup Using Systemd in Linux - GoLinuxCloud
MENU
Home » Linux » How To » How to run script at startup using systemd in Linux
Table of Contents
Step 1: Overview on systemd
Step 2: Create Sample Script
Step 3: Create systemd unit file for different scenario
Step 3.1: Run script at startup with systemd after network becomes reachable
Step 3.2: Run script at startup with systemd after all systemd services are loaded
Step 3.3: Run script at startup with systemd after login prompt appears
Step 4: Verify the systemd unit file configuration
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
RHEL/CentOS 7/8 Linux.
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
There can be various scenarios when you expect a script or command to be called at startup such as
https://www.golinuxcloud.com/run-script-at-startup-boot-without-cron-linux/ 1/8
4/2/2020 How to run script at startup using systemd in Linux | GoLinuxCloud
In this article I will cover below two topics as they are almost similar
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.
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.
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.
Enable the service to make sure this is called automatically after reboot
Advertisements
https://www.golinuxcloud.com/run-script-at-startup-boot-without-cron-linux/ 2/8
4/2/2020 How to run script at startup using systemd in Linux | GoLinuxCloud
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.
[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.
[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
[Service]
Type=simple
https://www.golinuxcloud.com/run-script-at-startup-boot-without-cron-linux/ 3/8
4/2/2020 How to run script at startup using systemd in Linux | GoLinuxCloud
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 in
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 un
jobs are dispatched (see man systemd.service for even more choices -- e.g., Type=oneshot can
TimeoutStartSec= When a service doesn't signal start-up completion within TimeoutStartSec, systemd considers t
for long-running shell scripts it is essential to modify TimeoutStartSec or disable the timeo
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
Now our configuration is in place to run script at startup in CentOS/RHEL 7/8 Linux.
HINT:
Advertisements
You can also use systemctl list-jobs within the script to monitor the list of active jobs when the script was executed at startup.
Now post reboot of the Linux node we can see that our startup_script.sh is running in the background
https://www.golinuxcloud.com/run-script-at-startup-boot-without-cron-linux/ 4/8
4/2/2020 How to run script at startup using systemd in Linux | GoLinuxCloud
Also in some time we start getting the broadcast messages from root user.
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.
Related Articles:
https://www.golinuxcloud.com/run-script-at-startup-boot-without-cron-linux/ 5/8
4/2/2020 How to run script at startup using systemd in Linux | GoLinuxCloud
How to fix “another app is currently holding the yum lock” error
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment
Post Comment
Search
Advertisements
JOIN US
To get latest articles on
Linux, Cloud, Containers, Networking, Storage, Virtualization and many more topics
SUBSCRIBE NOW
2K+ USERS
https://www.golinuxcloud.com/run-script-at-startup-boot-without-cron-linux/ 6/8
4/2/2020 How to run script at startup using systemd in Linux | GoLinuxCloud
Also Read
100+ Java Interview Questions And Answers For Freshers & Experienced-2
How To Check And Set Java Timezone (Update Tzdata For OpenJDK) Using Tzupdater
How To Restrict Or Allow Ssh Only From Certain Users, Groups Or Hosts In Linux
How To Exclude Some Accounts From Being Locked After Multiple Incorrect Password
https://www.golinuxcloud.com/run-script-at-startup-boot-without-cron-linux/ 7/8
4/2/2020 How to run script at startup using systemd in Linux | GoLinuxCloud
Linux Sftp Restrict User To Specific Directory | Setup Sftp Chroot Jail 100+ Java Interview Questions And Answers For Freshers & Experienced-
2
Linux Lvm Snapshot Backup And Restore Tutorial RHEL/CentOS 7/8
100+ GIT Interview Questions And Answers For Developers
6 Easy Steps To Setup Offline Two Factor Authentication In Linux
Shell Scripting Interview Questions With Answers
Unix Or Linux Sticky Bit | Detailed Beginners Guide
100+ Java Interview Questions And Answers For Freshers & Experienced-
Tutorial: Beginners Guide On Linux Memory Management
1
Copyright © 2020 | Disclaimer | Hosted By Hostinger Home About Me Subscribe Advertise With Us Contact Us
https://www.golinuxcloud.com/run-script-at-startup-boot-without-cron-linux/ 8/8