Using systemd Timers to Schedule Tasks on Ubuntu
Ubuntu includes a powerful scheduling system called systemd, and one of its most useful features is the systemd timer. Timers can launch commands, scripts, backups, maintenance jobs, and other services at a particular time or after a defined interval. They provide a modern alternative to traditional cron jobs while fitting neatly into the way Ubuntu starts and manages software.
A timer is normally paired with a systemd service. The timer decides when something should run, while the service describes what should happen. This separation makes scheduled tasks easier to inspect, start manually, log, and control without building complicated shell commands around them.
For everyday Ubuntu users, this is useful for tasks such as copying documents to an external drive, refreshing a local backup, cleaning temporary files, or running a script before the workday begins. It can also help small businesses, community groups, and home users maintain computers without relying on a desktop application remaining open.
The examples below use standard Ubuntu tools and work well on laptops, desktops, and servers. Times are interpreted using the computer’s configured timezone, which matters in Australia when daylight saving changes between states such as New South Wales and Queensland.
How Systemd Timers Work
A systemd timer is a unit file ending in .timer. It contains scheduling rules and normally points to a matching .service unit. For example, daily-report.timer can activate daily-report.service at a chosen time each day.
The service contains the command or script that should run. A simple service might look like this:
[Unit]
Description=Create the daily report
[Service]
Type=oneshot
ExecStart=/usr/local/bin/create-daily-report.sh
The Type=oneshot setting tells systemd that the service performs a task and then exits. The timer can be stored as /etc/systemd/system/daily-report.timer and may contain:
[Unit]
Description=Run the daily report each morning
[Timer]
OnCalendar=*-*-* 07:30:00
Persistent=true
Unit=daily-report.service
[Install]
WantedBy=timers.target
The names before and after the suffix should match. If the timer is called daily-report.timer, systemd will look for daily-report.service by default. The explicit Unit= line makes that relationship clear and is helpful when the names differ.
Creating A Scheduled Service
Before creating a timer, write and test the command that does the actual work. For example, create a script at /usr/local/bin/create-daily-report.sh:
#!/bin/bash
set -euo pipefail
mkdir -p /var/local/reports
date '+Report created: %Y-%m-%d %H:%M:%S' \
>> /var/local/reports/daily.log
Make the file executable:
sudo chmod 755 /usr/local/bin/create-daily-report.sh
A script used by systemd should use full paths wherever possible. The environment provided to a system service is smaller than the environment in an interactive terminal, so commands that work because of a customised shell PATH may fail when launched by a timer.
Create the service file with:
sudo nano /etc/systemd/system/daily-report.service
A service can run as root when it needs access to system directories, but that should not be the default for ordinary tasks. Add a User= line when the job only needs a normal account’s permissions. For a user called alex, the service might include:
[Service]
Type=oneshot
User=alex
ExecStart=/home/alex/bin/create-daily-report.sh
Test the service directly before scheduling it:
sudo systemctl daemon-reload
sudo systemctl start daily-report.service
sudo systemctl status daily-report.service
Testing the service first separates script problems from scheduling problems. If the command fails when started manually, changing the timer will not fix it.
Choosing Calendar And Interval Schedules
The OnCalendar= option is best for human-readable times. These examples show common patterns:
OnCalendar=Mon..Fri 08:00
OnCalendar=*-*-01 02:15:00
OnCalendar=Sat *-*-* 10:30
The first runs from Monday to Friday at 8 am, the second runs at 2:15 am on the first day of every month, and the third runs on Saturdays at 10:30 am. Several OnCalendar= lines can be included if a task needs multiple run times.
For regular intervals, use a monotonic timer instead. OnBootSec=15min runs 15 minutes after boot, while OnUnitActiveSec=6h runs six hours after the service was last activated. A timer could contain:
[Timer]
OnBootSec=15min
OnUnitActiveSec=6h
Unit=cache-refresh.service
This kind of schedule is useful for laptops that may boot at unpredictable times. It avoids assuming that the computer will be running at a particular clock time.
Use systemd-analyze calendar to check calendar expressions before saving them:
systemd-analyze calendar "Mon..Fri 08:00"
systemd-analyze calendar "*-*-01 02:15:00"
The command displays the next matching time according to the system’s timezone. This is especially useful in Australia, where Adelaide, Melbourne, and Sydney observe daylight saving while Brisbane, Darwin, and Perth generally do not.
Enabling And Controlling Timers
After creating or changing unit files, reload systemd’s configuration:
sudo systemctl daemon-reload
Start a timer immediately with:
sudo systemctl start daily-report.timer
To have it start automatically whenever Ubuntu boots, enable it:
sudo systemctl enable daily-report.timer
It is common to use both commands together:
sudo systemctl enable --now daily-report.timer
Enabling a timer does not run the task straight away. It arranges for the timer to be started during boot. The --now option starts it immediately as well, which is generally convenient after installation.
Check all active timers with:
systemctl list-timers
For system-wide timers, use:
sudo systemctl list-timers --all
The output includes the next activation, the previous activation, and the service controlled by each timer. The --all option also displays timers that are installed but currently inactive.
To stop or disable a timer:
sudo systemctl disable --now daily-report.timer
Stopping prevents future activations until the timer is started again. Disabling removes its automatic boot start, but it does not delete the unit files.
Handling Missed Runs And Laptop Sleep
Computers are often asleep or powered off when a scheduled time arrives. Persistent=true helps with calendar timers by recording the last activation time and running the associated service after the computer becomes available again.
For example:
[Timer]
OnCalendar=*-*-* 07:30:00
Persistent=true
If the laptop was asleep at 7:30 am, the service can run after it wakes. This is valuable for Ubuntu laptops used on a train commute in Sydney, at a café in Fremantle, or between meetings in Melbourne, where regular sleep and wake cycles are normal.
Persistent=true does not queue every missed run. It generally causes one activation if the scheduled time passed while the timer was inactive. That makes it suitable for daily backups or reports, but less suitable for workloads where every interval must be processed.
A timer cannot run while the computer is completely powered off unless a compatible hardware wake-up arrangement is configured. For essential backups, consider running the job on an always-on home server, a small office machine, or a hosted system rather than depending on a laptop that may be in a backpack.
Calendar timers can also behave differently around daylight saving transitions. Avoid scheduling critical work during ambiguous or missing local times, such as the hour affected by a clock change. A time like 3:30 am is often less troublesome than the transition period itself, though the precise behaviour depends on the local timezone rules.
Viewing Logs And Diagnosing Failures
Systemd sends service output to the journal by default. View the recent output for a scheduled service with:
sudo journalctl -u daily-report.service
To show only entries from the current boot:
sudo journalctl -u daily-report.service -b
Follow new entries in real time with:
sudo journalctl -u daily-report.service -f
The timer has its own status information:
systemctl status daily-report.timer
systemctl list-timers --all
If the timer appears active but the task does not work, inspect the service rather than only the timer. Common causes include a missing executable permission, an incorrect file path, a script that expects a graphical desktop, or a command that is unavailable in systemd’s restricted environment.
A script that writes to a user’s home directory should also account for ownership and permissions. If the service runs as root, newly created files may be owned by root and become awkward to edit from the desktop account. Running the service as the intended user is usually cleaner for personal files.
For syntax and dependency problems, use:
systemd-analyze verify /etc/systemd/system/daily-report.service
systemd-analyze verify /etc/systemd/system/daily-report.timer
This can identify malformed unit files before the timer is enabled.
Using User Timers Instead Of System Timers
System timers are stored in /etc/systemd/system/ and usually require administrator access. User timers are stored in ~/.config/systemd/user/ and can be created without sudo. They are ideal for tasks involving personal documents, application data, or files owned by one account.
Create the directory first:
mkdir -p ~/.config/systemd/user
A user service might contain:
[Unit]
Description=Back up personal notes
[Service]
Type=oneshot
ExecStart=%h/bin/backup-notes.sh
The matching timer could be:
[Unit]
Description=Back up personal notes every evening
[Timer]
OnCalendar=*-*-* 20:00:00
Persistent=true
[Install]
WantedBy=timers.target
Save these as notes-backup.service and notes-backup.timer in the user unit directory. Then reload and enable them without sudo:
systemctl --user daemon-reload
systemctl --user enable --now notes-backup.timer
systemctl --user list-timers
By default, user services may stop when the user logs out. To allow them to run while the user is not logged in, enable lingering:
loginctl enable-linger "$USER"
Use this carefully, particularly on a shared computer, because it allows user-level background services to continue running after logout.
Making Scheduled Jobs Safer
A scheduled script should be predictable, quiet when successful, and clear when it fails. Use set -euo pipefail where appropriate, quote file paths, and write useful error messages. If a task modifies files, test it against a temporary directory before pointing it at important documents.
The service can impose basic limits. For example:
[Service]
Type=oneshot
User=alex
ExecStart=/home/alex/bin/backup-notes.sh
Nice=10
PrivateTmp=true
Nice=10 gives the job lower CPU scheduling priority, which can reduce disruption during work hours. PrivateTmp=true gives the service an isolated temporary directory. These settings are not a complete security policy, but they can reduce unnecessary access.
Avoid placing passwords directly in unit files or shell scripts. Use appropriate file permissions, protected configuration files, or a secrets manager. A backup command that includes a cloud token in plain text can expose that token through file backups, support requests, or careless permissions.
Finally, review the job occasionally. Confirm that the destination still has space, that old files are rotated, and that the timer is producing recent journal entries. A green timer status only proves that systemd scheduled the service; it does not prove that the operation achieved its purpose.
A practical workflow is to create the service, run it manually, validate the output, add the timer, inspect the next activation with systemctl list-timers, and review the journal after the first scheduled run. Once those checks pass, systemd timers provide a dependable, transparent way to automate Ubuntu tasks without adding another desktop application or relying on a terminal window staying open.