Crontab basic commands
In Linux, Cron is a daemon/service that executes shell commands periodically on a given schedule. Cron is driven by a crontab, a configuration file that holds details of what commands are to be run along with a timetable of when to run them.
- Create a cron file
$ crontab -e
It will open crontab file. A new blank file if you do not have an existing crontab file.
2. List active crontab
$ crontab -l
3. crontab syntax
A crontab file has six fields for specifying minute, hour, day of month, month, day of week and the command to be run at that interval.
4. crontab examples
* * * * * #Runs every minute
30 * * * * #Runs at 30 minutes past the hour
45 7 * * * #Runs at 7:45 am every day
45 17 * * * #Runs at 5:45 pm every day
00 2 * * 0 #Runs at 2:00 am every Sunday
00 2 * * 7 #Runs at 2:00 am every Sunday
00 2 * * Sun #Runs at 2:00 am every Sunday
30 9 1 * * #Runs at 9:30 am on the first day of every month
00 0–23/2 02 08 * #Runs every other hour on the 2nd of August
5. Special strings that can be used in crontab
@reboot #Runs at boot
@yearly #Runs once a year [0 0 1 1 *]
@annually #Runs once a year [0 0 1 1 *]
@monthly #Runs once a month [0 0 1 * *]
@weekly #Runs once a week [0 0 * * 0]
@daily #Runs once a day [0 0 * * *]
@midnight #Runs once a day [0 0 * * *]
@hourly #Runs once an hour [0 * * * *]
6. Multiple commands
&& can be used to run multiple commands consecutively.
@daily command_01 && command_02
7. Specifying a user’s crontab file to use
$crontab -u username file
8. Removing a crontab file
$crontab -r