Schedule tasks with cron on Linux

Write crontab entries with confidence: the five time fields, working examples, logging output, avoiding overlapping runs and checking that jobs actually ran.

  • Beginner
  • Linux VPS
  • 3 min read

By RunRacks Team · Published

cron runs commands on a schedule: nightly backups, cleanup scripts, report emails or a framework's scheduler. Each user has their own crontab.

1. Make sure cron is installed

It is on Ubuntu and Debian. Minimal AlmaLinux and Rocky images may need it:

Terminal
sudo dnf install -y cronie
sudo systemctl enable --now crond

2. Edit your crontab

Terminal
crontab -e          # your user's jobs
sudo crontab -e     # root's jobs
crontab -l          # list jobs

3. Read the five time fields

Text
# ┌ minute (0-59)
# │ ┌ hour (0-23)
# │ │ ┌ day of month (1-31)
# │ │ │ ┌ month (1-12)
# │ │ │ │ ┌ day of week (0-7, Sunday is 0 or 7)
# │ │ │ │ │
  * * * * * command

Examples:

Text
*/5 * * * *  /usr/local/bin/check-site.sh          # every 5 minutes
30 2 * * *   /usr/local/bin/backup.sh              # every day at 02:30
0 9 * * 1    /usr/local/bin/weekly-report.sh       # Mondays at 09:00
0 0 1 * *    /usr/local/bin/monthly-cleanup.sh     # first day of each month
@reboot      /usr/local/bin/on-boot.sh             # once at startup

Times use the server's time zone. Check it with timedatectl, and see Set the time zone.

4. Log the output

By default, output is mailed to the user or lost. Send it to a log file instead, including errors:

Text
30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

5. Avoid the three classic mistakes

  1. PATH is minimal. cron does not load your shell profile. Use full paths (/usr/bin/php, not php); command -v php shows the full path.
  2. `%` is special. In a crontab line, % means a new line. Escape it as \%, for example $(date +\%F).
  3. Overlapping runs. If a job can take longer than its interval, wrap it in flock so a second copy never starts while the first runs:
Text
*/5 * * * * /usr/bin/flock -n /tmp/sync.lock /usr/local/bin/sync.sh

System-wide jobs

Files in /etc/cron.d/ have an extra user field after the time fields:

Text
15 3 * * * www-data /usr/bin/php /var/www/example.com/artisan schedule:run

Check that jobs ran

Terminal
grep CRON /var/log/syslog          # Ubuntu, or Debian with rsyslog
journalctl -u cron --since today   # Debian 12 / Ubuntu
sudo tail /var/log/cron            # AlmaLinux / Rocky

Troubleshooting

  • Job never runs: check the line with crontab -l, and that the script is executable (chmod +x).
  • Runs by hand but not in cron: almost always PATH or environment variables. Use full paths and set needed variables at the top of the script.
  • Runs at the wrong hour: the server time zone differs from yours, or cron was not restarted after a time zone change.

Next steps

Was this guide helpful?

Still stuck?

Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.

Linux VPS

Deploy a Node.js app with PM2 behind Nginx

Install a Node.js LTS release, keep your app running with PM2 across crashes and reboots, and serve it through Nginx with HTTPS.

  • Intermediate
  • Linux VPS
  • 3 min read

Linux VPS

Create an SFTP-only user limited to one folder

Give someone file access over SFTP without a shell, locked inside one folder. Configure internal-sftp with a Match block, set the chroot ownership right and test with WinSCP.

  • Advanced
  • Linux VPS
  • 5 min read