Back up every night, clean a folder every week, restart a program every morning: Linux can run such tasks for you at fixed times. This page shows cron, the classic and simplest way, and systemd timers, which log every run and catch up on runs missed while the server was off. The steps were tested on Ubuntu 26.04 LTS.
Tasks run in the time zone of the server. Check it with timedatectl; servers often run in UTC.
1. cron
Every user has their own list of tasks, the crontab. Open yours:
crontab -e
The first time, choose an editor; nano is the easiest. Each line is one task: five time fields, then the command.
# minute hour day-of-month month day-of-week command 30 3 * * * /home/alex/backup.sh >> /home/alex/backup.log 2>&1
This runs backup.sh every day at 03:30 and writes its output to backup.log. Save and close the editor; cron uses the new list at once.
*/5 * * * * every 5 minutes 0 * * * * every hour, on the hour 0 4 * * 1 every Monday at 04:00 0 2 1 * * on the 1st of every month at 02:00
- Use full paths for programs and files: cron does not read your login settings.
- Always send the output somewhere, as above (
>> file 2>&1). Otherwise errors are lost. crontab -llists your tasks;crontab -rdeletes all of them.
Check that a task ran: cron writes every start to the system log.
journalctl -u cron --since today
Each start appears there as a line with (alex) CMD (…).
2. systemd Timers
A timer needs two files: a service that says what to run, and a timer that says when. Run as root. The service, /etc/systemd/system/cleanup.service:
[Unit] Description=Weekly cleanup [Service] Type=oneshot User=alex ExecStart=/home/alex/cleanup.sh
The timer, with the same name, /etc/systemd/system/cleanup.timer:
[Unit] Description=Run the weekly cleanup [Timer] OnCalendar=Mon *-*-* 04:00 Persistent=true [Install] WantedBy=timers.target
Persistent=true runs the task after a restart if its time passed while the server was off. Start the timer:
systemctl daemon-reload systemctl enable --now cleanup.timer systemctl list-timers cleanup.timer
list-timers shows when the task runs next (NEXT, LEFT) and when it ran last. Each run, with its output, is in the log:
journalctl -u cleanup.service
Not sure about a time expression? systemd-analyze calendar shows when it fires next:
systemd-analyze calendar "Mon *-*-* 04:00"
Other examples: daily (every day at midnight), hourly, *-*-* 03:30 (every day at 03:30), *:0/15 (every 15 minutes).
To run the task once by hand, for a test: systemctl start cleanup.service. To stop the schedule: systemctl disable --now cleanup.timer.
Which One?
- cron: quick and simple, one line per task, also as a normal user.
- Timers: every run in the log with its result, catch-up after downtime, and the task can use all options of a service. See also Keep a Program Running on Linux.