GGistDev

Scheduling

Run commands at specific times using cron, at, or systemd timers.

Cron basics

Crontab format: min hour day month weekday (use crontab -e).

# Every day at 02:30
30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

Environment is minimal; set PATH explicitly and use absolute paths.

Cron helpers

  • crontab -l list, crontab -e edit
  • Use MAILTO=user@example.com to receive outputs via mail (if configured)
  • Use run-parts for directory-based schedules

at (one-off jobs)

echo "/usr/local/bin/report.sh" | at 09:00
atq    # list jobs
atrm 1 # remove job 1

systemd timers (Linux)

# myjob.timer
[Unit]
Description=Run my job hourly

[Timer]
OnCalendar=hourly
Persistent=true

[Install]
WantedBy=timers.target
# myjob.service
[Unit]
Description=My job

[Service]
Type=oneshot
ExecStart=/usr/local/bin/myjob.sh

Enable with systemctl enable --now myjob.timer.

Caveats

  • Cron may not load your interactive shell config; source needed env explicitly
  • Use locks to avoid overlapping runs (e.g., flock)
  • Log outputs; rotate logs

Summary

  • Use cron for recurring, at for one-offs, and systemd timers for robust service-managed schedules