Create Cron Jobs on Linux
Cron is a time-based scheduler commonly used on Linux and Unix-like systems. It can run backups, cleanup scripts, reports, maintenance commands, and other recurring tasks automatically.
Cron Syntax
A user crontab entry has five schedule fields followed by the command:
* * * * * /path/to/command
- - - - -
| | | | |
| | | | +----- day of week (0-7, Sunday=0 or 7)
| | | +------- month (1-12)
| | +--------- day of month (1-31)
| +----------- hour (0-23)
+------------- minute (0-59)Edit Your User Crontab
crontab -eFor example, run a backup every day at 02:30:
30 2 * * * /home/user/bin/backup.sh
List current entries:
crontab -lCommon Schedules
Every hour:
0 * * * * /path/to/script.sh
Every day at midnight:
0 0 * * * /path/to/daily-task.sh
Every Monday at 17:00:
0 17 * * 1 /path/to/weekly-task.sh
On the 1st and 15th of every month:
0 0 1,15 * * /path/to/task.sh
System-Wide Cron Entries
/etc/crontab and files under /etc/cron.d/ usually include an additional user field:
30 2 * * * root /usr/local/sbin/system-backup
Use distribution conventions and appropriate permissions when creating system-wide entries.
Cron Has a Minimal Environment
A command that works in your interactive shell can fail under cron because the working directory, PATH, shell, and environment variables may differ. Prefer absolute paths and set required environment variables explicitly.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
30 2 * * * /home/user/bin/backup.sh >> /home/user/log/backup.log 2>&1
Troubleshooting
If a job does not run:
- verify it appears in
crontab -l; - use absolute paths;
- check script permissions and the interpreter shebang;
- capture stdout and stderr to a log while debugging;
- inspect cron logs, commonly via
journalctl,/var/log/syslog, or/var/log/crondepending on the distribution.
Conclusion
Cron is a simple and dependable scheduler for recurring Linux tasks. Define the schedule carefully, use explicit paths and environments, and capture errors so automated jobs remain observable.