Cron and Automation
crontab syntax, scheduling tasks, @daily/@weekly, logging cron output
Skills taught: linux.cron, linux.crontab, linux.scheduling, linux.automation
What is cron?
Cron is the classic Unix job scheduler — a background daemon that runs commands at a specific time or interval. Every user has their own crontab (cron table), a file listing what to run and when. It is built into every Linux distribution and macOS.
Crontab commands
# Edit your crontab (opens in $EDITOR)
crontab -e
# List current crontab entries
crontab -l
# Remove your entire crontab (careful!)
crontab -r
# Edit crontab for a specific user (root only)
crontab -u alice -e
# View cron logs (Ubuntu/Debian)
grep CRON /var/log/syslog
# View cron logs (macOS)
log show --predicate 'process == "cron"' --last 1h
Cron syntax
# Format:
# ┌─────────── minute (0-59)
# │ ┌───────── hour (0-23)
# │ │ ┌─────── day of month (1-31)
# │ │ │ ┌───── month (1-12)
# │ │ │ │ ┌─── day of week (0-7, 0 and 7 = Sunday)
# │ │ │ │ │
# * * * * * command to run
# Special values:
# * = every (every minute, every hour, etc.)
# , = list (1,5,10 = at 1, 5, and 10)
# - = range (1-5 = 1 through 5)
# / = step (*/15 = every 15 minutes)
# Examples:
# Run at 2:30am every day:
30 2 * * * /home/alice/backup.sh
# Run every 15 minutes:
*/15 * * * * /usr/bin/check_health.sh
# Run at 9am Monday through Friday:
0 9 * * 1-5 /home/alice/send_report.sh
# Run at midnight on the 1st of every month:
0 0 1 * * /home/alice/monthly_cleanup.sh
# Run every hour:
0 * * * * /home/alice/sync.sh
@shortcuts — readable aliases
# Instead of cryptic timing, use @shortcuts:
@reboot /home/alice/startup.sh # run once at system boot
@hourly /home/alice/check.sh # = 0 * * * *
@daily /home/alice/backup.sh # = 0 0 * * * (midnight)
@weekly /home/alice/cleanup.sh # = 0 0 * * 0 (Sunday midnight)
@monthly /home/alice/report.sh # = 0 0 1 * *
@yearly /home/alice/annual.sh # = 0 0 1 1 *
# @reboot is especially useful for starting services
# after a reboot without systemd
Logging and debugging cron jobs
# By default, cron emails output to the user (often silently discarded)
# Always redirect output explicitly:
# Redirect both stdout and stderr to a log file
30 2 * * * /home/alice/backup.sh >> /home/alice/logs/backup.log 2>&1
# Discard all output (use only when you're sure the script works)
30 2 * * * /home/alice/backup.sh > /dev/null 2>&1
# Log with timestamps using tee
30 2 * * * /home/alice/backup.sh 2>&1 | tee -a /home/alice/logs/backup.log
# Important: cron runs with a minimal PATH (/usr/bin:/bin)
# If your script uses tools like node, python, git — use FULL paths
# or set PATH at the top of your crontab:
PATH=/usr/local/bin:/usr/bin:/bin:/home/alice/.local/bin
30 2 * * * backup.sh # now can find node, git, etc.
# Or use full path inside the script:
/usr/local/bin/node /home/alice/app/sync.js
# Tip: always test the script manually first before scheduling it:
bash /home/alice/backup.sh
Tip: Use full paths for commands in crontab entries. Cron runs with a very minimal PATH — commands like node, python3, or git that work in your terminal won't be found by cron unless you specify their full path (e.g., /usr/local/bin/node) or set a PATH= variable at the top of the crontab file.
Frequently Asked Questions
My cron job works when I run the script manually but not as a cron job. Why?
Cron runs with a minimal environment: no .bashrc, no .profile, and a very limited PATH. Fixes: (1) Use full absolute paths for all commands. (2) Add PATH=/usr/local/bin:/usr/bin:/bin and any other directories at the top of your crontab. (3) Source your profile at the start of the script: source /home/user/.bashrc. Always redirect output to a log file to see what error cron is reporting.
What is the difference between cron and systemd timers?
systemd timers are the modern Linux alternative. They support: missed-run catchup (runs a job even if the system was off at the scheduled time), per-job logging in the systemd journal (journalctl -u mytimer.timer), better error handling, and dependency management. For new Linux services, prefer systemd timers. For macOS, use launchd plist files (or a modern wrapper like launchd-crontab). Cron is still widely used for its simplicity and cross-platform availability.