Tuesday, January 11, 2011

Crontab for Fedora / RedHat / CentOS / 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:

# crontab -l


You can create a crontab file by entering the following terminal command:

 # 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:

#################################################################
#minute (0-59),                                                 #
#|      hour (0-23),                                            #
#|      |       day of the month (1-31),                        #
#|      |       |       month of the year (1-12),               #
#|      |       |       |       day of the week (0-6 with 0=Sun)#
#|      |       |       |       |       commands                #
#################################################################


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

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 * * * *]

You can use multiple commands for the same crontab:

@daily &&


Specifying a crontab file to use

# crontab -u

Example:
# crontab -u tux ~/crontab

-would set Tux's crontab file to that of the file named "crontab" residing in Tux's home directory.

To remove a crontab file for current user:
# crontab -r

No comments: