Linux Crontab Command
The crontab command in Linux is used to schedule tasks to run at specific times or intervals. These tasks are commonly referred to as "cron jobs."
Crontab Syntax
The basic syntax for a crontab entry is:
* * * * * command_to_execute
- - - - -
| | | | |
| | | | +---- Day of the week (0 - 7) [Both 0 and 7 represent Sunday]
| | | +------ Month (1 - 12)
| | +-------- Day of the month (1 - 31)
| +---------- Hour (0 - 23)
+------------ Minute (0 - 59)
Examples
1. Run a script every day at 5:00 AM
0 5 * * * /path/to/script.sh
2. Run a command every Monday at 10:30 AM
30 10 * * 1 /path/to/command
3. Run a task every 15 minutes
*/15 * * * * /path/to/task
4. Run a backup script on the 1st of every month at midnight
0 0 1 * * /path/to/backup.sh
Editing the Crontab File
To edit the crontab file for the current user, use the command:
crontab -e
To view the current user's crontab entries, use:
crontab -l
To remove the current user's crontab, use:
crontab -r
Special Strings
Crontab also supports special strings for common schedules:
@reboot- Run once at startup@yearlyor@annually- Run once a year (0 0 1 1 *)@monthly- Run once a month (0 0 1 * *)@weekly- Run once a week (0 0 * * 0)@dailyor@midnight- Run once a day (0 0 * * *)@hourly- Run once an hour (0 * * * *)
Conclusion
The crontab command is a powerful tool for automating repetitive tasks in Linux. By understanding its syntax and options, you can efficiently schedule tasks to suit your needs.