Archive · Linux

Scheduling Jobs: cron and systemd Timers

Run tasks on a schedule, and learn why jobs that work in a terminal fail in cron.

1 min read

  • Ubuntu 18.04.3 LTS
  • cron 3.0
  • systemd 237

crontab -e edits your personal schedule. The five fields are minute, hour, day of month, month and day of week.

# m  h  dom mon dow  command
30 2  *   *   *      /opt/scripts/backup.sh >> /var/log/backup.log 2>&1
*/5 * *   *   *      /opt/scripts/healthcheck.sh

30 2 * * * means every day at 02:30. System-wide jobs go in /etc/cron.d/ and need a user name before the command.

Common reasons a cron job silently fails: cron runs with a minimal PATH, so use full paths; there is no terminal, so redirect output to a log; and the server time zone may not be yours (timedatectl).

systemd offers timers as an alternative: a .timer unit triggers a .service, with logs in the journal and options such as Persistent=true to run a missed job after downtime. Cron is simpler for one-liners; timers suit jobs you want supervised.