Execute Task periodically

Cron, however, fails if your system happens to not be running when the appropriate execution time occurs.
Anacron insures that the task will be executed when your system is again active. However, the anacron frequency of execution can be no less than daily.

Systemd timers offer the best of both cron and anacron. - Systemd Timers for Scheduling Tasks / systemd/Timers

The modern option is to use a systemd timer unit. This requires creating a systemd unit which defines the job you want to periodically run, and a systemd.timer unit defining the schedule for the job. - SO

Running job as regular user

$ systemctl --user status "*timer"  # list timers
$ systemctl --user list-timers      # summary of next programmed execution

$ systemctl --all list-timers		# list all active/inactive

Create timer

in $HOME/.config/systemd/user

my-job.service

[Unit]
Description=Job that needs periodic execution

[Service]
ExecStart=/path/to/your/script

my-job.timer

[Unit]
Description=Timer that periodically triggers my-job.service

[Timer]
OnCalendar=minutely

Then enable the newly created units, and start the timer:

$ systemctl --user enable my-job.service my-job.timer
$ systemctl --user start my-job.timer

To verify that the timer is set:

$ systemctl --user list-timers
NEXT                         LEFT     LAST                         PASSED UNIT         ACTIVATES
Wed 2016-11-02 14:07:00 EAT  19s left Wed 2016-11-02 14:06:37 EAT  3s ago my-job.timer my-job.service

journalctl -xe should show log entries of the job being run. systemctl --user status disk-usage.service can help diagnose issue as well (eg: bad path of script or shell)

Written on June 22, 2022, Last update on October 22, 2023
linux-system systemd automation service timer