Cron jobs are automated tasks scheduled to run at specific times on Unix-like operating systems. They are widely used for system maintenance, backups, and periodic operations.
Basic Syntax
A cron job is defined using a cron expression that specifies the schedule. The format is:
* * * * * command_to_execute
- Minute (0-59)
- Hour (0-23)
- Day of the month (1-31)
- Month (1-12)
- Day of the week (0-6, where 0 is Sunday)
Example:
0 3 * * * /path/to/your/script.sh
This runs script.sh
every day at 3:00 AM.
Practical Examples
Daily Backup
0 2 * * * /backup_script.sh
📝 Note: Replace
/backup_script.sh
with your actual backup command.Weekly Report
0 0 * * 0 /generate_weekly_report.sh
This executes the script every Sunday at midnight.
Monthly System Check
0 0 1 * * /system_check.sh
Runs on the 1st of every month.
Best Practices
- Use absolute paths for scripts.
- Test commands with
crontab -l
before saving. - Redirect output to log files for debugging:
* * * * * /script.sh >> /var/log/cron.log 2>&1
- Avoid spaces in cron expressions.
Related Tutorials
For advanced scheduling techniques, check out our guide on Scheduling Tools.