Sub topic 4
Scheduling with systemd Service and Timer Units
Assume you are comfortable with the Linux command line, shell scripts, users, permissions, processes, and basic log reading. This chapter uses systemd, the service manager used by many Linux distributions.
A systemd timer schedules an activation. It normally activates a service with the same base name. For example, backup.timer starts backup.service. The service contains the actual command. Keeping scheduling separate from execution makes the job easier to test and inspect.
Create the service unit
Create /etc/systemd/system/backup.service:
[Unit]
Description=Nightly database backup
[Service]
Type=oneshot
User=backup
ExecStart=/usr/local/bin/backup.sh
TimeoutStartSec=1hType=oneshot tells systemd that the service performs one operation and then exits. User=backup runs the command without root privileges. The backup user must have the permissions required by the script, but no more.
ExecStart should use an absolute path. Do not assume that shell aliases, the current directory, or your interactive PATH will exist. TimeoutStartSec prevents a stuck backup from running forever.
The service can be tested without waiting for the schedule:
sudo systemctl daemon-reload
sudo systemctl start backup.service
systemctl status backup.service
sudo journalctl -u backup.service --since todayA successful oneshot service may show inactive (dead) after it finishes. That is normal. Check the exit status and journal output rather than expecting the service to remain active.
Create the timer unit
Create /etc/systemd/system/backup.timer:
[Unit]
Description=Run the nightly database backup
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
RandomizedDelaySec=5min
[Install]
WantedBy=timers.targetOnCalendar uses a calendar expression. This expression schedules the timer every day at 02:30 according to the system's local time zone. Persistent=true records the last activation. If the machine was off at the scheduled time, systemd starts the service after the machine returns, instead of silently discarding that run.
RandomizedDelaySec=5min adds a delay of up to five minutes. This can prevent many machines from starting expensive work at exactly the same instant.
Enable and inspect the timer
Reload unit files, then enable and start the timer:
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers backup.timerenable makes the timer start during boot. --now starts it immediately as well. list-timers shows the next and last activation times.
Start the service manually when testing. This separates script errors from scheduling errors:
sudo systemctl start backup.service
sudo journalctl -u backup.service -n 50 --no-pagerUse systemctl status backup.timer when the timer itself appears inactive or failed. Use journalctl -u backup.service for the command's output and exit status.
Choose timer settings deliberately
Use OnCalendar for wall-clock schedules such as nightly backups. Use a monotonic setting such as OnBootSec=10min when the job should run relative to boot. A timer can also combine calendar and monotonic triggers, but document the intended behavior so operators can predict it.
A timer is a good fit when you need journal integration, missed-run handling, timeouts, dependency ordering, or resource controls. Cron can be simpler for small, established scripts, but it provides fewer service-management features by default.
Exercise
Which unit should contain the backup command, and what does Persistent=true change?
Show answer
The command belongs in backup.service. Persistent=true causes systemd to activate the service after boot when a scheduled activation was missed while the machine was off.