How to Use Crontab -- Syntax, Examples & Common Errors
In this tutorial, you'll learn about How to Use Crontab. We cover key concepts, practical examples, and best practices.
The Problem
You set up a cron job with crontab -e but it never runs, or it runs but fails silently. The script works fine when you run it manually, but cron produces different results because of limited environment variables and missing paths.
Quick Fix
Step 1: Understand the five-field syntax
Crontab entries use five time fields followed by the command:
# * * * * * command
# | | | | |
# | | | | +-- Day of week (0-6, 0=Sunday)
# | | | +---- Month (1-12)
# | | +------ Day of month (1-31)
# | +-------- Hour (0-23)
# +---------- Minute (0-59)
Example -- run every day at 2:30 AM:
30 2 * * * /home/user/scripts/backup.sh
Step 2: Use absolute paths
Cron runs with a minimal PATH. Always use full paths:
# Wrong -- script may not be found
30 2 * * * backup.sh
# Right
30 2 * * * /home/user/scripts/backup.sh
Step 3: Redirect output to a log file
Capturing output helps debug failures:
30 2 * * * /home/user/scripts/backup.sh >> /home/user/logs/backup.log 2>&1
Step 4: Add PATH and environment variables
Set environment variables at the top of the crontab:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAILTO=admin@example.com
30 2 * * * /home/user/scripts/backup.sh
Step 5: Test commands with common crontab patterns
Run every minute (for testing):
* * * * * /home/user/scripts/test.sh
Run every hour:
0 * * * * /home/user/scripts/hourly.sh
Run every Sunday at midnight:
0 0 * * 0 /home/user/scripts/weekly.sh
Step 6: Check cron logs
View cron execution history:
sudo journalctl -u cron --no-pager -n 20
Or check syslog:
grep CRON /var/log/syslog | tail -10
Alternative Solutions
Use systemd timers instead of cron
Modern alternative with better logging and dependency control:
sudo systemctl edit --full my-timer.timer
Use special @reboot syntax
Run a command once at system boot:
@reboot /home/user/scripts/startup.sh
Common Mistakes to Avoid
Not using absolute paths in cron jobs. Cron runs with a minimal PATH. Always use full paths to executables and scripts.
Forgetting that % is special in crontab. The % character must be escaped with a backslash in cron commands. date +\%Y-\%m-\%d.
Not redirecting output to a log file. Without output redirection, cron errors go to the system mail, which may never be read.
Pro Tips
Use MAILTO to receive cron output. Set MAILTO=admin@example.com at the top of your crontab to receive job output via email.
Use flock to prevent overlapping jobs. Wrap your cron command with flock -n /tmp/myapp.lock to ensure only one instance runs at a time.
Use crontab guru for syntax validation. Visit crontab.guru to check your cron expressions before adding them to production systems.
Prevention
- Always use absolute paths for scripts and commands in cron jobs.
- Redirect stdout and stderr to a log file for troubleshooting.
- Test scripts manually with the same environment cron uses:
env -i /bin/bash -c 'source /etc/profile && /path/to/script.sh'.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro