Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Open Your Crontab

crontab -e

This opens your personal crontab in an editor; on Debian and Ubuntu you may be asked to pick one the first time. Add a line, save, and close: the job is active at once. Each user has a separate crontab; sudo crontab -e edits root's. If the command is missing, install cron on Debian and Ubuntu or cronie on AlmaLinux and Rocky Linux. To see what is already scheduled, see how to list cron jobs.

The Five Fields

*/5 * * * * /usr/local/bin/check-site
| | | | |
| | | | day of week (0-7, 0 and 7 are Sunday)
| | | month (1-12)
| | day of month (1-31)
| hour (0-23)
minute (0-59)
  • *: every value.
  • ,: a list, as in 1,15.
  • -: a range, as in 1-5.
  • /: a step, as in */10 for every tenth.

Times are in the server's time zone; check it with timedatectl.

Common Schedules

FieldsRuns
*/5 * * * *Every 5 minutes
0 * * * *Every hour, on the hour
30 2 * * *Daily at 02:30
0 3 * * 0Sundays at 03:00
0 4 1 * *First day of each month
0 9-17 * * 1-5Hourly, 09:00 to 17:00, weekdays

If both day of month and day of week are set, the job runs when either one matches.

Shortcuts

Instead of the five fields you can write @hourly, @daily, @weekly, @monthly, or @yearly, which run at the start of that period, and @reboot, which runs once when cron starts after a boot.

Cron's Minimal Environment

Cron does not load your .bashrc. It runs commands with /bin/sh, a short PATH, and your home folder as working directory. A command that works in your terminal can fail in cron for that reason alone.

  • Use absolute paths: /usr/bin/php, not php. command -v php shows the full path.
  • Or set PATH at the top of the crontab: PATH=/usr/local/bin:/usr/bin:/bin.
  • Escape percent signs: cron treats % as a line break, so write \%, or move the command into a script.
  • No secrets in the crontab: let the script read passwords from a file with mode 600.

Log the Output

30 2 * * * /home/deploy/bin/backup.sh >> /home/deploy/logs/backup.log 2>&1

>> appends standard output to the log and 2>&1 adds the errors. The log folder must exist and be writable by the crontab's owner.

Stop Overlapping Runs with flock

*/5 * * * * /usr/bin/flock -n /tmp/sync.lock /home/deploy/bin/sync.sh

If the previous run still holds the lock, -n makes the new one exit at once. The lock is released when the job ends; the file itself can stay.

Mail with MAILTO

MAILTO=YOUR_EMAIL

Placed at the top of the crontab, it sends the output of every job by mail, if the server can send mail; see how to send email from the command line. MAILTO="" turns it off.

When a Job Did Not Run

  1. Check the service: systemctl status cron on Debian and Ubuntu, systemctl status crond on AlmaLinux and Rocky Linux.
  2. Read the log: journalctl -u cron or journalctl -u crond; on AlmaLinux and Rocky Linux also /var/log/cron.
  3. Check that the script is executable: chmod +x.
  4. Run it with an empty environment: env -i /bin/sh -c '/home/deploy/bin/backup.sh'.
  5. Make sure the last line of the crontab ends with a newline.

systemd Timers as an Alternative

A systemd timer does the same job with logs in the journal, catch-up runs after downtime, and resource limits. See systemd unit files; a complete timer example is in the BorgBackup guide.

Was this answer helpful?
Loading...