How to Use Crontab in Linux: Schedule Tasks Easily
Use
crontab -e to edit your cron jobs in Linux. Each line in the crontab file defines a scheduled task with time fields and the command to run. Save and exit to activate the schedule.Syntax
The crontab syntax has five time fields followed by the command to run:
- Minute (0-59)
- Hour (0-23)
- Day of Month (1-31)
- Month (1-12)
- Day of Week (0-7, where 0 and 7 mean Sunday)
Use * to mean 'every' value for that field. After these fields, write the command you want to run.
bash
* * * * * /path/to/command
Example
This example runs a script every day at 7:30 AM. It shows how to schedule a simple task using crontab.
bash
30 7 * * * /home/user/backup.sh
Output
No output when saved; the script runs automatically at 7:30 AM daily.
Common Pitfalls
Common mistakes include:
- Not using full paths for commands or scripts.
- Forgetting to make scripts executable (
chmod +x script.sh). - Editing crontab with the wrong user (use
crontab -eas the correct user). - Not redirecting output, causing cron emails or silent failures.
Always test your commands manually before scheduling.
bash
Wrong: * * * * * backup.sh Right: * * * * * /home/user/backup.sh
Quick Reference
| Field | Allowed Values | Description |
|---|---|---|
| Minute | 0-59 | Minute of the hour |
| Hour | 0-23 | Hour of the day |
| Day of Month | 1-31 | Day of the month |
| Month | 1-12 | Month of the year |
| Day of Week | 0-7 | Day of the week (0 or 7 is Sunday) |
Key Takeaways
Use
crontab -e to edit your scheduled tasks safely.Each crontab line has five time fields followed by the command to run.
Always use full paths and make scripts executable to avoid errors.
Test commands manually before adding them to crontab.
Use
* to mean 'every' unit in time fields.