Introduction
Cron is a time-based job scheduler in Unix-like operating systems.
Users can schedule scripts or commands to run at specific intervals using the cron daemon. It is commonly used for system maintenance, automation, and recurring tasks.
Cron Syntax
A cron job follows a specific syntax consisting of five time fields followed by the command to be executed:
* * * * * /path/to/command
- - - - -
| | | | |
| | | | +---- Day of the week (0 - 7) [0 and 7 represent Sunday]
| | | +------ Month (1 - 12)
| | +-------- Day of the month (1 - 31)
| +---------- Hour (0 - 23)
+------------ Minute (0 - 59)
Special Characters
- * → Any value (wildcard, runs every possible value)
- , → List of values (e.g., 1,5,10 runs on days 1, 5, and 10)
- - → Range of values (e.g., 1-5 runs from day 1 to day 5)
- / → Step values (e.g., */5 means every 5 minutes)
Common Cron Jobs Examples
- Run a script every day at midnight:
- Run a command every Monday at 8 AM:
- Run a backup job every 6 hours:
- Restart a service every Sunday at 3 AM:
- Run a script on the first day of every month at 5 AM:
0 0 * * * /path/to/script.sh
0 8 * * 1 /path/to/command
0 */6 * * * /path/to/backup.sh
0 3 * * 0 systemctl restart apache2
0 5 1 * * /path/to/monthly_task.sh
Managing Cron Jobs
Adding and Editing Cron Jobs
- Edit cron jobs:
crontab -e
This opens the user’s crontab file in the default editor.
Viewing Scheduled Jobs
- List cron jobs for the current user:
- List cron jobs for a specific user (requires sudo access):
crontab -l
sudo crontab -u username -l
Removing Cron Jobs
- Remove all cron jobs for the current user:
- Remove a specific cron job by editing the crontab (crontab -e) and deleting the relevant line.
crontab -r
Logging and Debugging
- Check cron logs to verify if jobs are executing:
- Redirect output to a log file for debugging:
- Check user-specific cron logs:
cat /var/log/syslog | grep CRON
0 2 * * * /path/to/script.sh >> /var/log/script.log 2>&1
journalctl -u cron --no-pager
Environment Considerations
Cron jobs run with a minimal environment. Unlike interactive shell sessions, they do not inherit user-specific configurations. This can lead to issues where commands work in the terminal but fail in cron.
Using Environment Variables
To ensure a consistent execution environment:
PATH=/usr/local/bin:/usr/bin:/bin
0 1 * * * /path/to/script.sh
Alternatively, define environment variables inside the script:
#!/bin/bash
export PATH=/usr/local/bin:/usr/bin:/bin
python3 /path/to/script.py
Using Absolute Paths
Always use absolute paths when specifying files or scripts:
/home/user/scripts/myscript.sh
Troubleshooting
1. Cron Job Not Running?
- Ensure the script has execution permissions:
- Use absolute paths in the script.
- Check if the cron service is running:
- Ensure the script works manually:
- If the job uses sudo, try adding it explicitly:
chmod +x /path/to/script.sh
systemctl status cron
/path/to/script.sh
0 3 * * * sudo /path/to/script.sh
2. Checking User Privileges
Cron jobs may fail due to permission issues. Ensure the correct user is running the job:
whoami
If root privileges are needed, add the job under the root user’s crontab:
sudo crontab -e
3. Using Debugging Techniques
- Run cron jobs with verbose logging:
- Check email notifications for cron job failures:
bash -x /path/to/script.sh > /tmp/debug.log 2>&1
(Cron sends output to the user’s mail by default.)
Conclusion
Cron is a powerful tool for task automation. By understanding its syntax, logging outputs, and troubleshooting issues, users can ensure reliable execution of scheduled tasks.