Disclosure: this page contains affiliate links. If you buy through them we may earn a commission at no extra cost to you.

Cron runs commands on a schedule — backups, cleanup, certificate renewal, or any repeatable task. Here’s how to use it correctly.

The crontab format

A cron line has five time fields plus the command:

minute hour day month weekday command

Examples:

  • 0 3 * * * — daily at 3:00 AM
  • */15 * * * * — every 15 minutes
  • 0 2 * * 0 — weekly on Sunday

Add a job

Edit your crontab:

crontab -e

Add a line, for example a nightly database backup:

0 3 * * * mysqldump myapp | gzip > /backups/myapp-$(date +\%F).sql.gz

Prefer /etc/cron.d for servers

System jobs are cleaner in /etc/cron.d/ because each file names a user and can live in version control:

0 3 * * * root /usr/local/bin/backup.sh

Test before you trust it

  • Use absolute paths — cron’s PATH is minimal.
  • Log output by appending >> /var/log/myjob.log 2>&1.
  • Escape % as \% in the command.

Any VPS can run cron; it costs almost nothing. RackNerd’s 1 GB plan at $21.99/yr is enough for backups and scheduled tasks.

See RackNerd's current deals