Categories
SysOps

How to delay the start of the systemd service

Delay the start of the systemd service at boot.

Inspect service that you want to delay at boot.

$ sudo systemctl status redis-server
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-09-12 21:23:37 UTC; 12s ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 907 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 2311)
     Memory: 7.2M
        CPU: 114ms
     CGroup: /system.slice/redis-server.service
             └─907 /usr/bin/redis-server 127.0.0.1:6379

Sep 12 21:23:37 bullseye systemd[1]: Starting Advanced key-value store...
Sep 12 21:23:37 bullseye systemd[1]: Started Advanced key-value store.

Create a timer trigger to start service 1 minute after the system boot.
Notice, that the timer uses the same service name.

$ cat <<EOF | sudo tee /etc/systemd/system/redis-server.timer
[Unit]
Description=Timer for the redis-server service

[Timer]
OnBootSec=1min

[Install]
WantedBy=timers.target
EOF

Reload systemd configuration.

$ sudo systemctl daemon-reload

Disable service at boot.

$ sudo systemctl disable redis-server
Removed /etc/systemd/system/multi-user.target.wants/redis-server.service.
Removed /etc/systemd/system/redis.service.

Enable service timer.

$ sudo systemctl enable redis-server.timer 
Created symlink /etc/systemd/system/timers.target.wants/redis-server.timer → /etc/systemd/system/redis-server.timer.

Restart container.

$ uptime
 21:34:09 up 0 min,  1 user,  load average: 0.00, 0.00, 0.00

The service will be disabled by default.

$ sudo systemctl status redis-server
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
TriggeredBy: ● redis-server.timer
       Docs: http://redis.io/documentation,
             man:redis-server(1)

Timer will trigger it automatically.

$ sudo systemctl list-timers 
NEXT                        LEFT          LAST                        PASSED     UNIT                         ACTIVATES
Sun 2021-09-12 21:34:46 UTC 30s left      n/a                         n/a        redis-server.timer           redis-server.service
Sun 2021-09-12 21:48:46 UTC 14min left    n/a                         n/a        systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Mon 2021-09-13 00:00:00 UTC 2h 25min left Sun 2021-09-12 11:57:28 UTC 9h ago     logrotate.timer              logrotate.service
Mon 2021-09-13 00:00:00 UTC 2h 25min left Sun 2021-09-12 11:57:28 UTC 9h ago     man-db.timer                 man-db.service
Mon 2021-09-13 00:02:04 UTC 2h 27min left Fri 2021-09-10 09:41:16 UTC 2 days ago fstrim.timer                 fstrim.service
Mon 2021-09-13 00:28:54 UTC 2h 54min left Sun 2021-09-12 15:20:37 UTC 6h ago     apt-daily.timer              apt-daily.service
Mon 2021-09-13 06:22:58 UTC 8h left       Sun 2021-09-12 12:00:52 UTC 9h ago     apt-daily-upgrade.timer      apt-daily-upgrade.service
Sun 2021-09-19 03:10:52 UTC 6 days left   Sun 2021-09-12 11:57:36 UTC 9h ago     e2scrub_all.timer            e2scrub_all.service

8 timers listed.
Pass --all to see loaded but inactive timers, too.

Wait for the remaining time and inspect the service.

$ sudo systemctl status redis-server
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-09-12 21:34:46 UTC; 5s ago
TriggeredBy: ● redis-server.timer
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 562 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 2311)
     Memory: 10.1M
        CPU: 104ms
     CGroup: /system.slice/redis-server.service
             └─562 /usr/bin/redis-server 127.0.0.1:6379

Sep 12 21:34:46 bullseye systemd[1]: Starting Advanced key-value store...
Sep 12 21:34:46 bullseye systemd[1]: Started Advanced key-value store.