Using Crontab in Ubuntu
Using Crontab in Ubuntu
Crontab can run scripts at regular intervals and perform various tasks. Those intervals can be from 1 minute to 1 year, repeatedly.
To list current crontabs:
sudo crontab -l
You can create a crontab file by entering the following terminal command:
sudo crontab -e
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:
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
Some examples:
* * * * * #Runs every minute
*/5 * * * * #Runs at every 5 minutes
30 * * * * #Runs at 30 minutes past the hour
45 6 * * * #Runs at 6:45 am every day
45 18 * * * #Runs at 6:45 pm every day
00 1 * * 0 #Runs at 1:00 am every Sunday
00 1 * * 7 #Runs at 1:00 am every Sunday
00 1 * * Sun #Runs at 1:00 am every Sunday
30 8 1 * * #Runs at 8:30 am on the first day of every month
00 0-23/2 02 07 * #Runs every other hour on the 2nd of July
*/5 * * * *
30 * * * *
45 6 * * *
45 18 * * *
00 1 * * 0
00 1 * * 7
00 1 * * Sun
30 8 1 * *
00 0-23/2 02 07 *
You can also use some special strings:
@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 * * * *]
@yearly
@annually
@monthly
@weekly
@daily
@midnight
@hourly
You can use multiple commands for the same crontab:
@daily <command_01> && <command_02>
Specifying a crontab file to use
sudo crontab -u
Example:
sudo crontab -u tux ~/crontab
-would set Tuxs crontab file to that of the file named "crontab" residing in Tuxs home directory.
To remove a crontab file for current user:
sudo crontab -r
Resources: Ubuntu.com, Crunchbang, PHP cronfile Sitepoint
download file now