What Are Cron Jobs?
Cron is a time-based job scheduler in Unix-like operating systems. It runs scripts or commands at specified intervals, like every minute, daily at 2 AM, or every Monday. If you've ever needed to automate repetitive tasks, cron is your friend.
The Basics: crontab
The crontab command manages your cron jobs. To edit your user's crontab, run:
crontab -e
This opens your crontab file in the default editor. Each line defines a job. The syntax is:
* * * * * command_to_run
The five asterisks represent, in order:
- Minute (0-59)
- Hour (0-23)
- Day of month (1-31)
- Month (1-12)
- Day of week (0-7, where both 0 and 7 are Sunday)
Simple Examples
Run a script every day at 3:30 AM:
30 3 * * * /home/user/backup.sh
Run every 15 minutes:
*/15 * * * * /home/user/check.sh
Run at midnight on the first of every month:
0 0 1 * * /home/user/monthly_report.py
Run every weekday (Monday to Friday) at 9 AM:
0 9 * * 1-5 /home/user/weekday_task.sh
Making Cron Jobs Easy to Manage
Instead of cramming commands into a single line, write a small script and call that. This makes testing and debugging much easier.
Create a script, say myjob.sh:
#!/bin/bash
# Do something useful
echo "Job ran at $(date)" >> /var/log/myjob.log
Make it executable:
chmod +x myjob.sh
Then add to crontab:
*/5 * * * * /home/user/myjob.sh
Handling Output and Errors
By default, cron emails the output of your job. If you don't check email, that's useless. Redirect output to a log file:
*/5 * * * * /home/user/myjob.sh >> /var/log/myjob.log 2>&1
Or discard it entirely:
*/5 * * * * /home/user/myjob.sh > /dev/null 2>&1
Environment Variables
Cron runs with a minimal environment. Your PATH may not include where your scripts or commands live. Always use absolute paths, or set the PATH at the top of the crontab.
PATH=/usr/local/bin:/usr/bin:/bin
*/5 * * * * /home/user/myjob.sh
Common Pitfalls
-
Forgot the shebang: If your script doesn't have
#!/bin/bash(or similar), cron might not know how to run it. - Permissions: Ensure the script is executable and readable by the user running cron.
-
Line endings: If you edit crontab on Windows and upload, carriage returns can break it. Use
dos2unixor a proper editor. -
Time zones: Cron uses the system's time zone. If you need a different one, you can set
CRON_TZin the crontab (if supported).
Testing Cron Jobs
Before relying on cron, test your script manually:
bash /home/user/myjob.sh
Check the log after cron runs:
cat /var/log/myjob.log
You can also list your current cron jobs with:
crontab -l
Remove a job with crontab -e and delete the line, or clear all with crontab -r (use with caution).
Advanced Scheduling Tricks
Run a job only on specific days of the week and month combination. For example, run every Friday the 13th:
0 0 13 * 5 /home/user/friday13.sh
Note: This runs if either the day of month is 13 OR the day of week is Friday. That's how cron works (OR logic). If you need AND logic, you'll have to check inside the script.
For more complex schedules, you can use tools like cronitor or systemd timers, but for most cases, plain cron is enough.
Conclusion
Cron jobs are a powerful way to automate routine tasks. Start with simple schedules, redirect output to logs, and always test your scripts manually first. Once you get the hang of the five-field syntax, you'll wonder how you lived without it.
Happy automating!
Top comments (0)