Theo's Corner
dev / irl / thoughts
← Back
tech

What a cron job is and how to use one

Theo|Jul 2026|~4 min read

Cron is Linux's built-in task scheduler. It runs commands automatically on a schedule — every minute, every hour, every day at 3am, whatever you need. Once you know how to use it, you'll find yourself reaching for it constantly.

The crontab syntax

Every cron job is a line in a crontab file with five time fields followed by the command:

# ┌─ minute (0-59)
# │ ┌─ hour (0-23)
# │ │ ┌─ day of month (1-31)
# │ │ │ ┌─ month (1-12)
# │ │ │ │ ┌─ day of week (0-7, 0 and 7 are Sunday)
# │ │ │ │ │
  * * * * * command-to-run

A * means "every." So * * * * * runs every minute. 0 3 * * * runs at 3am every day. 0 3 * * 0 runs at 3am every Sunday.

Editing your crontab

crontab -e   # edit your crontab
crontab -l   # list current cron jobs

Real examples I actually use

# Database backup every day at 2am
0 2 * * * pg_dump -U postgres mydb > /backups/db-$(date +\%Y\%m\%d).sql

# Sync backups to NAS every night at 3am
0 3 * * * rsync -az /backups/ user@nas:/server-backups/

# Clear old log files weekly
0 4 * * 0 find /var/log/myapp -name "*.log" -mtime +30 -delete
Use full paths for everything in cron jobs — cron runs with a minimal environment and doesn't have your normal PATH. /usr/bin/pg_dump not just pg_dump. Use which pg_dump to find the full path.

Checking if cron jobs ran

Cron logs to syslog by default. Check with:

grep CRON /var/log/syslog

If a job is failing silently, redirect its output to a log file to see what's happening:

0 2 * * * /usr/bin/pg_dump -U postgres mydb > /backups/db.sql 2>> /var/log/backup-errors.log