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

In this article I will share systemd unit file and examples to run script after 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 after N minutes of boot based on the value on N. let us verify this using some examples:

 

Some more articles on similar topic:

 

Overview on systemd.timer

  • A unit configuration file whose name ends in ".timer" encodes information about a timer controlled and supervised by systemd, for timer-based activation.
  • The common configuration items are configured in the generic "[Unit]" and "[Install]" sections. The timer specific configuration options are configured in the "[Timer]" section.
  • For each timer file, a matching unit file must exist, describing the unit to activate when the timer elapses.
  • By default, a service by the same name as the timer (except for the suffix) is activated. Example: a timer file foo.timer activates a matching service foo.service. The unit to activate may be controlled by Unit=
  • Note that in case the unit to activate is already active at the time the timer elapses it is not restarted, but simply left running. There is no concept of spawning new service instances in this case.
  • Due to this, services with RemainAfterExit= set (which stay around continuously even after the service's main process exited) are usually not suitable for activation via repetitive timers, as they will only be activated once, and then stay around forever.
  • Timer units automatically gain a Before= dependency on the service they are supposed to activate.
  • Timer units will automatically have dependencies of type Requires= and After= on sysinit.target, a dependency of type Before= on timers.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 timers in your environment use

# systemctl list-timers

 

Step 1: Create sample script

To run script after N minutes of boot with systemd we will create a dummy script which will put some content in an empty file after 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 after N minutes of boot

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

 

Step 2.1: OnBootSec vs OnStartupSec

  • OnBootSec= Defines a timer relative 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 timer relative to when the service manager was first started. For system timer 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:

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

 

Step 3: Sample systemd unit timer file to run script after N minutes of boot

timer is a unit configuration file whose name ends in ".timer" encodes information about a timer controlled and supervised by systemd, for timer-based activation.

[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

Here for OnBootSec or OnStartupSec,

  • The arguments to the directives are time spans configured in seconds. Example: OnBootSec=50 means 50s after boot-up.
  • The argument may also include time units. Example: OnBootSec=5h 30min means 5 hours and 30 minutes after boot-up.
  • For details about the syntax of time spans supported with OnBootSec= or OnStartupSec= follow man page of systemd.timer

Refresh the systemd configuration files

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

Disable the systemd unit service file as this should not start automatically which is the idea behind this article. We want this service to run script after N minutes of boot based on the timer 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 timer file so that this will run post boot and will then trigger the mapped systemd unit service file based on the timer 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/system/run-script-with-delay.timer.

Next reboot the node.

 

Step 4: Verify the systemd unit file configuration

Post reboot when we check the status of run-script-with-delay.timer, here observe the highlighted section where it shows the next trigger which is planned after 5 minutes which we have configured with OnBootSec in the timer 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 time is hit, the service will perform it's defined task which here is to run script after 5 minutes of boot. As you see now the trigger is showing "not applicable" for timer 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
  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 after 5 minutes of system boot without using any cron job.

 

Lastly I hope the steps from the article to run script after N minutes of boot without cronjob on 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 with certain pre-defined delay after 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 section 'Timer'

Leave a Comment

Please use shortcodes <pre class=comments>your code</pre> for syntax highlighting when adding code.