You are on page 1of 10

3/3/2020 How to run script after N minutes of boot with systemd CentOS/RHEL 7/8 | GoLinuxCloud

MENU

Home » Linux » How To » How to run script a er N minutes of boot with systemd CentOS/RHEL 7/8

HOW TO LINUX TIPS AND TRICKS

How to run script a er N minutes of boot with systemd CentOS/RHEL 7/8

Table of Contents
Overview on systemd. mer
Step 1: Create sample script
Step 2: Sample systemd unit service file to run script a er N minutes of boot
Step 2.1: OnBootSec vs OnStartupSec
Step 3: Sample systemd unit mer file to run script a er N minutes of boot
Step 4: Verify the systemd unit file configura on

HostGator© O cial Site


Powerful Web Hosting for Only a $3. Great Bundle with Every Plan. Learn More!

hostgator.com OPEN

In this ar cle I will share systemd unit file and examples to run script a er N minutes of boot using systemd (not with cron job).
Ideally such task can also be done using cron jobs but since we already have systemd then why should we depend on cron job.

Using OnBootSec= or OnStartupSec= we can easily run script a er N minutes of boot based on the value on N. let us verify

this using some examples:

Some more ar cles on similar topic:

How to execute a command or script with systemd at shutdown only and not at reboot in Linux
https://www.golinuxcloud.com/run-script-after-n-minutes-of-boot-systemd/ 1/10
3/3/2020 How to run script after N minutes of boot with systemd CentOS/RHEL 7/8 | GoLinuxCloud

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

Overview on systemd. mer

A unit configura on file whose name ends in “ .timer ” encodes informa on about a mer controlled and supervised by
systemd, for mer-based ac va on.

The common configura on items are configured in the generic “ [Unit] ” and “ [Install] ” sec ons. The mer specific
configura on op ons are configured in the “ [Timer] ” sec on.

For each mer file, a matching unit file must exist, describing the unit to ac vate when the mer elapses.

By default, a service by the same name as the mer (except for the suffix) is ac vated. Example: a mer file foo. mer
ac vates a matching service foo.service. The unit to ac vate may be controlled by Unit=

Note that in case the unit to ac vate is already ac ve at the me the mer elapses it is not restarted, but simply le
running. There is no concept of spawning new service instances in this case.

Due to this, services with RemainAfterExit= set (which stay around con nuously even a er the service’s main process
exited) are usually not suitable for ac va on via repe ve mers, as they will only be ac vated once, and then stay around
forever.

Timer units automa cally gain a Before= dependency on the service they are supposed to ac vate.

Timer units will automa cally have dependencies of type Requires= and After= on sysinit.target , a dependency of
type Before= on mers.target, as well as Conflicts= and Before= on shutdown.target to ensure that they are stopped
cleanly prior to system shutdown.

To list all the available mers in your environment use

https://www.golinuxcloud.com/run-script-after-n-minutes-of-boot-systemd/ 2/10
3/3/2020 How to run script after N minutes of boot with systemd CentOS/RHEL 7/8 | GoLinuxCloud

# systemctl list-timers

Step 1: Create sample script

To run script a er N minutes of boot with systemd we will create a dummy script which will put some content in an empty file
a er 5 minutes of boot.

[root@centos-8 ~]# cat /tmp/delay_script.sh


#!/bin/bash

echo "Hello Deepak, I am running late" >> /tmp/file

Provide executable permission to the script

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

Step 2: Sample systemd unit service file to run script a er N minutes of boot

Here we must create a systemd unit service file and a matching systemd unit mer file to run script a er N minutes of boot. We
can either use OnBootSec= or OnStartupSec= to run script with delay a er boot in Linux

Step 2.1: OnBootSec vs OnStartupSec

OnBootSec= Defines a mer rela ve to when the machine was booted up. In containers, for the system manager instance,
this is mapped to OnStartupSec= , making both equivalent.

OnStartupSec= Defines a mer rela ve to when the service manager was first started. For system mer units this is very
similar to OnBootSec= as the system service manager is generally started very early at boot. It’s primarily useful when
configured in units running in the per-user service manager, as the user service manager is generally started on first login
only, not already during boot.

[root@centos-8 ~]# cat /etc/systemd/system/run-script-with-delay.service


[Unit]
Description=Run script at startup

[Service]
Type=oneshot
ExecStart=/tmp/delay_script.sh
TimeoutStartSec=0

NOTE:

https://www.golinuxcloud.com/run-script-after-n-minutes-of-boot-systemd/ 3/10
3/3/2020 How to run script after N minutes of boot with systemd CentOS/RHEL 7/8 | GoLinuxCloud

You will end up with “ systemctl failing with unknown section Timer ” error if you put [Timer] sec on in .service
unit file. [Timer] must be used only in .timer unit file

Step 3: Sample systemd unit mer file to run script a er N minutes of boot

mer is a unit configura on file whose name ends in “ .timer ” encodes informa on about a mer controlled and supervised by

systemd, for mer-based ac va on.

[root@centos-8 ~]# cat /etc/systemd/system/run-script-with-delay.timer


[Unit]
Description="Run script after 5 minutes of boot"

[Timer]
OnBootSec=5min

[Install]
WantedBy=default.target

HINT:

Adver sements

Veeam Availability
Suite
Backup and Recovery for all
workloads – VMware, Hyper-V,
Windows, AWS and more!
Veeam Software

The unit to ac vate when this mer elapses is provided by using Unit= under [Timer] . Here since our systemd service and
mer file are of same name i.e. run-script-with-delay , we have not defined any Unit= in the mer unit file. In case your
unit service and mer file names are different then provide the mapped service file name with Unit= in the . mer file under
[Timer].

Here for OnBootSec or OnStartupSec,

https://www.golinuxcloud.com/run-script-after-n-minutes-of-boot-systemd/ 4/10
3/3/2020 How to run script after N minutes of boot with systemd CentOS/RHEL 7/8 | GoLinuxCloud

The arguments to the direc ves are me spans configured in seconds. Example: OnBootSec=50 means 50s a er boot-up.

The argument may also include me units. Example: OnBootSec=5h 30min means 5 hours and 30 minutes a er boot-up.

For details about the syntax of me spans supported with OnBootSec= or OnStartupSec= follow man page of
systemd. mer

Refresh the systemd configura on files

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

Disable the systemd unit service file as this should not start automa cally which is the idea behind this ar cle. We want this
service to run script a er N minutes of boot based on the mer value.

[root@centos-8 ~]# systemctl disable run-script-with-delay.service


Removed /etc/systemd/system/default.target.wants/run-script-with-delay.service.

Next enable the systemd unit mer file so that this will run post boot and will then trigger the mapped systemd unit service file
based on the mer value.

[root@centos-8 ~]# systemctl enable run-script-with-delay.timer


Created symlink /etc/systemd/system/default.target.wants/run-script-with-delay.timer → /etc/systemd/syst

Next reboot the node.

Step 4: Verify the systemd unit file configura on

Post reboot when we check the status of run-script-with-delay.timer , here observe the highlighted sec on where it
shows the next trigger which is planned a er 5 minutes which we have configured with OnBootSec in the mer unit file.

[root@centos-8 ~]# systemctl status run-script-with-delay.timer


● run-script-with-delay.timer - "Run script after 5 minutes of boot"
Loaded: loaded (/etc/systemd/system/run-script-with-delay.timer; enabled; vendor preset: disabled)
Active: active (waiting) since Thu 2020-01-16 14:34:34 IST; 27s ago
Trigger: Thu 2020-01-16 14:39:30 IST; 4min 27s left

Once the trigger me is hit, the service will perform it’s defined task which here is to run script a er 5 minutes of boot. As you
see now the trigger is showing “not applicable” for mer unit

[root@centos-8 ~]# systemctl status run-script-with-delay.timer


● run-script-with-delay.timer - "Run script after 5 minutes of boot"
Loaded: loaded (/etc/systemd/system/run-script-with-delay.timer; enabled; vendor preset: disabled)
Active: active (elapsed) since Thu 2020-01-16 14:34:34 IST; 5min ago

https://www.golinuxcloud.com/run-script-after-n-minutes-of-boot-systemd/ 5/10
3/3/2020 How to run script after N minutes of boot with systemd CentOS/RHEL 7/8 | GoLinuxCloud

Trigger: n/a

Jan 16 14:34:34 centos-8.example.com systemd[1]: Started "Run script after 5 minutes of boot".

Also verify the content of our output file.

[root@centos-8 ~]# cat /tmp/file


Hello Deepak, I am running late

So as expected our script /tmp/delay_script.sh was called a er 5 minutes of system boot without using any cron job.

Adver sements

Veeam Availability
Suite
Backup and Recovery for all
workloads – VMware, Hyper-V,
Windows, AWS and more!
Veeam Software

Lastly I hope the steps from the ar cle to run script a er N minutes of boot without cronjob on CentOS/RHEL 7/8 Linux was
helpful. So, let me know your sugges ons and feedback using the comment sec on.

Related Searches: How to execute script with certain pre-defined delay a er boot up in Linux. How to execute a script at startup
with 5 minutes delay using systemd (not cronjob) in CentOS/RHEL 7/8 Linux. How to fix systemctl failing with unknown sec on
‘Timer’

Related tags : systemd ps_and_tricks

Previous Post Next Post

https://www.golinuxcloud.com/run-script-after-n-minutes-of-boot-systemd/ 6/10
3/3/2020 How to run script after N minutes of boot with systemd CentOS/RHEL 7/8 | GoLinuxCloud
How to run script with systemd at shutdown How to read user input during boot stage
only (not at reboot) with systemd in Linux

Related Ar cles:

How to kill or disconnect hung ssh session in Linux

How to prevent user from using old password (or re-using) again in Linux

How to Install TripleO Undercloud (Openstack) on RHEL 7

Linux Boot Process Explained Step by Step in Detail

How to implement password policy in RHEL Linux with examples

How to check and disable transparent hugepages ( CentOS / RHEL 7 )

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

https://www.golinuxcloud.com/run-script-after-n-minutes-of-boot-systemd/ 7/10
3/3/2020 How to run script after N minutes of boot with systemd CentOS/RHEL 7/8 | GoLinuxCloud

Post Comment

Search

Google Custom Search.. Search

Adver sements

Veeam Availability Suite


Intelligent Backup and Recovery,
Cloud Mobility, Monitoring &
Analytics.

JOIN US
To get latest ar cles on

Linux, Cloud, Containers, Networking, Storage, Virtualiza on and many more topics

Directly delivered to your Inbox..

SUBSCRIBE NOW
2K+ USERS

Also Read

How To Implement Password Policy In RHEL Linux With Examples

How To Create Cluster Resource In HA Cluster (With Examples)

How To Check If Process Is Already Running In Bash (Shell Script) Linux

https://www.golinuxcloud.com/run-script-after-n-minutes-of-boot-systemd/ 8/10
3/3/2020 How to run script after N minutes of boot with systemd CentOS/RHEL 7/8 | GoLinuxCloud

Unregister RHEL 7 Using Subscrip on Manager And Remove System Profile

How To Create Or Configure Network Bridge In CentOS / RHEL 7/8

How To Mount Filesystem Without Fstab Using Systemd (CentOS/RHEL 7/8)

How To Disable Transparent HugePage Using GRUB2-EDITENV In RHEL 8

How To Enable Persistent Logging In Systemd-Journald Without Reboot

Configure Xfs Quota (User, Group And Project Quota) CentOS/RHEL 7/8

Steps To Add Linux To Windows AD Domain – Realm & Adcli (CentOS / RHEL 7)

How To Boot A System Into Rescue Mode Using ISO DVD (RHEL / CentOS 7)

100+ Java Interview Ques ons And Answers For Freshers & Experienced-1

How To Configure Secure Kerberized NFS Server ( RHEL / CentOS 7)

5 Commands To Check If Server Is Physical Or Virtual In Linux Or Unix

#4-ELK Stack: Configure Metricbeat 7.X To Monitor Elas csearch Cluster

https://www.golinuxcloud.com/run-script-after-n-minutes-of-boot-systemd/ 9/10
3/3/2020 How to run script after N minutes of boot with systemd CentOS/RHEL 7/8 | GoLinuxCloud

#1 Backup and
Recovery

Trusted by more than


350k customers
worldwide. Try a Free 30-
day Trial!

Recent Posts Interview Ques ons

Solved: Error Popula ng Transac on, Retrying RHEL/CentOS 7/8 Linux Interview Ques ons And Answers For Experienced Users

Bash Compare Strings | Bash Regex Match | Script Examples 20+ AWS Interview Ques ons And Answers For Freshers And
Experienced
5 Tools To Create Bootable Usb From Iso Linux Command Line And
Gui 100+ GIT Interview Ques ons And Answers For Developers

Create Ansible Role From Scratch | Ansible Roles Example 50 Maven Interview Ques ons And Answers For Freshers And
Experienced
Ansible Roles Directory Structure Overview | Beginners Guide
OpenStack Interview Ques ons And Answers (Mul choice)

Copyright © 2020 | Disclaimer | Hosted By Hos nger

Home About Me Subscribe Adver se With Us Contact Us

https://www.golinuxcloud.com/run-script-after-n-minutes-of-boot-systemd/ 10/10

You might also like