0
0
Linux CLIscripting~5 mins

systemd timers in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Systemd timers let you run tasks automatically at set times or intervals on Linux. They solve the problem of needing to run scripts or commands regularly without manual effort.
When you want to run a backup script every day at midnight without logging in.
When you need to check system health every hour automatically.
When you want to clean temporary files weekly without manual commands.
When you want to send a reminder email every Monday morning.
When you want to restart a service every day to keep it fresh.
Config File - mytask.timer
mytask.timer
[Unit]
Description=Run mytask.service every minute

[Timer]
OnBootSec=1min
OnUnitActiveSec=1min

[Install]
WantedBy=timers.target

[Unit] describes the timer unit and its purpose.

[Timer] sets when the timer triggers: 1 minute after boot and then every 1 minute.

[Install] makes the timer start when timers.target is active, enabling it to run automatically.

Commands
This command starts the timer immediately so it begins counting down to run the task.
Terminal
sudo systemctl start mytask.timer
Expected OutputExpected
No output (command runs silently)
This enables the timer to start automatically on system boot.
Terminal
sudo systemctl enable mytask.timer
Expected OutputExpected
Created symlink /etc/systemd/system/timers.target.wants/mytask.timer → /etc/systemd/system/mytask.timer.
This shows all timers on the system, including when they last ran and when they will run next.
Terminal
systemctl list-timers --all
Expected OutputExpected
NEXT LEFT LAST PASSED UNIT ACTIVATES Fri 2024-06-07 12:01:00 UTC 59s left Fri 2024-06-07 12:00:00 UTC 1min ago mytask.timer mytask.service 1 timers listed.
This shows the logs of the service that the timer triggers, so you can check if the task ran successfully.
Terminal
journalctl -u mytask.service
Expected OutputExpected
-- Logs begin at Fri 2024-06-07 11:00:00 UTC, end at Fri 2024-06-07 12:00:00 UTC. Jun 07 12:00:00 hostname systemd[1]: Started My Task Service. Jun 07 12:00:00 hostname mytask[1234]: Task executed successfully.
Key Concept

If you remember nothing else from systemd timers, remember: they schedule and run services automatically at set times without needing cron.

Common Mistakes
Not enabling the timer after creating it.
The timer won't start automatically on boot and only runs if started manually.
Always run 'sudo systemctl enable mytask.timer' to make it start on boot.
Creating a timer without a matching service file.
The timer triggers a service unit; without it, nothing runs.
Create a .service file with the same base name as the timer to define the task.
Using wrong time settings like OnActiveSec without understanding when it triggers.
The timer may not run when expected if time settings are misunderstood.
Use OnBootSec for delay after boot and OnUnitActiveSec for intervals after last run.
Summary
Create a .timer file to define when a task runs and a matching .service file to define what runs.
Use 'systemctl start' to run the timer now and 'systemctl enable' to run it on boot.
Check timers with 'systemctl list-timers' and logs with 'journalctl -u servicename'.