Systemd Timer

How to create a timer using systemd

systemd

126 Words

2026-07-07 10:57 +0200


Systemd execute a service at a planned time described in a timer. In order to run a script at 2 am we need to create a service and a timer.

Create a service file

#/etc/systemd/system/myscript.service
[Unit]
Description=Run a script daily at 2am
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/bash /myscript.sh
WorkingDirectory=/
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

Create a timer

#/etc/systemd/system/myscript.timer
[Unit]
Description=Run the service every day at 2am

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
RandomizedDelaySec=60

[Install]
WantedBy=timers.target

Reload the systemd daemon and enable the timer

sudo systemctl daemon-reload
sudo systemctl enable --now myscript.timer

Verify

sudo systemctl status myscript.timer
systemctl list-timers myscript.timer

We can run the timer right now and verify the logs:

sudo systemctl start myscript.service
sudo journalctl -u myscript.service | tail -20

Disable a timer :

sudo systemctl disable --now myscript.timer