Professional Documents
Culture Documents
MENU
Home » Linux » How To » 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.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
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
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.
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
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.
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
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.
[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
[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].
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
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.
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.
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.
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
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".
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’
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 prevent user from using old password (or re-using) again in Linux
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
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
Adver sements
JOIN US
To get latest ar cles on
Linux, Cloud, Containers, Networking, Storage, Virtualiza on and many more topics
SUBSCRIBE NOW
2K+ USERS
Also Read
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
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
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
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)
https://www.golinuxcloud.com/run-script-after-n-minutes-of-boot-systemd/ 10/10