Home Download 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


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 <command_01> && <command_02>


Specifying a crontab file to use

sudo crontab -u

Example:
sudo 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:

sudo crontab -r


Resources: Ubuntu.com, Crunchbang, PHP cronfile Sitepoint

5 comments:

hislitermos said...

Hi,
when i create crontab file and customized it, i cant save it. I press ctrl+x to close the file, it does not ask me to save it. ctrl+x shorcut has not an effect on my system. how come can i save it?
Thanks, Baris

Speedy Gonzales said...

I assumed that you saved the file in nano editor. If you use Ubuntu server you can try to use another editor like vi (you might need to install it using the command sudo apt-get install vi), and gkedit (installed default) for Ubuntu desktop.
You should also change the permissions if you have used a customized file. I hope this helps.

Steve Garner said...

sudo crontab -e
* * * * * /usr/bin/php5 /var/www/script.php

- works fine (every minute), but:

sudo crontab -e
*/5 * * * * /usr/bin/php5 /var/www/script.php

- does not work at all. Appreciate any thoughts.

Speedy Gonzales said...

make sure you're not having some code errors. test it first with your own command:

sudo /usr/bin/php5 -f /var/www/script.php

don't forget the -f parameter. for more info

man php

i've been testing your configuration and it worked just fine.

insight said...

Speedy Gonzales, THANK YOU, THANK YOU, THANK YOU!!! The -f flag is a life-saver! You are the best!

Post a Comment