You are on page 1of 8

4/2/2020 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

HOW TO LINUX TIPS AND TRICKS

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.

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

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

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

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

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 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

[root@centos-8 ~]# chmod u+x /tmp/startup_script.sh

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

Advertisements

[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-sta

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

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

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

Refresh the systemd configuration files and enable the service

[root@centos-8 ~]# systemctl daemon-reload


[root@centos-8 ~]# 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.

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

[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

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.

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

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 tags : systemd tips_and_tricks

Previous Post Next Post


How to read user input during boot stage with How to run script with systemd right before
systemd in Linux shutdown in Linux

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

Solved: vncserver service failed a configured resource limit was exceeded

How to configure secure Kerberized NFS Server ( RHEL / CentOS 7)

How to Encrypt Hard Disk (partition) using LUKS in Linux

How to fix “another app is currently holding the yum lock” error

Step-by-Step Tutorial: Configure software Linear RAID 0 in Linux

Step-by-Step Guide: Install & configure fail2ban sshd jail in Linux

Leave a Reply
Your email address will not be published. Required fields are marked *

Comment

Name * Email * Website

 Yes, add me to your mailing list

Post Comment

Search

Google Custom Search.. Search

Advertisements

JOIN US
To get latest articles on

Linux, Cloud, Containers, Networking, Storage, Virtualization and many more topics

Directly delivered to your Inbox..

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

How To Run Script With Systemd At Shutdown Only (Not At Reboot)

Linux Boot Process Explained Step By Step In Detail

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 Check If String Contains Numbers, Letters, Characters In Bash

6 Easy Steps To Setup Offline Two Factor Authentication In Linux

How To Set Up GFS2 With Clustering On Linux ( RHEL / CentOS 7 )

How To Configure VLAN With Bond Using Kickstart In Linux (CentOS/RHEL 7)

How To Exclude Some Accounts From Being Locked After Multiple Incorrect Password

How To Enable Persistent Logging In Systemd-Journald Without Reboot

How To Configure ISCSI Target And Initiator On CentOS/RHEL 7/8 Linux

Step-By-Step Tutorial: Configure LDAP Client To Authenticate With LDAP Server

How To Delete Iscsi Target From Initiator ( CentOS / RHEL 7 ) Linux

Step-By-Step Tutorial: Configure Software RAID 1 In Linux

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

Recent Posts Interview Questions

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

50 Maven Interview Questions And Answers For Freshers And


Experienced

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

You might also like