title

How to use Crontab in Ubuntu 10.04 LTS to automate tasks

Posted in "News"Ubuntu automate taks schedule job cronjob crontab

image 1

Sometimes you need to automate certain tasks that come with administrating a working server. All Linux variants such as Ubuntu have a built-in scheduler named Crontab that can take care of scheduling various tasks for you. 

 

1. How does it work?

Let us take an example to see how a task is built up. 

* * * * * /usr/bin/php5 /var/www/script.php

In this example we let a PHP script run every minute. The first part with the asterisks is the time and date a task needs to be run, specified in six fields starting with minute, hour, day of month, month, day of week and followed by the command to be run at that interval (in this case the PHP script). See below how to set a time and date for your task: 

*     *     *     *     *  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 of how to set up a time interval for your task: 

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

Special strings can also be used to run a task: 

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

 

2. Add a task to Crontab

Use the following command to open the crontab editor and start adding tasks: 

sudo crontab -e

 

3. List all existings crontab tasks 

sudo crontab -l

 

4. Example.

Below you find an example that I use to run DDclient to update my IP-address to DynDNS.org. The task is run every day at 4.45am. 

45 04 * * * /etc/init.d/ddclient restart

Note from Steve Holdoway (LinkedIn "Linux Users" group): "I think that the most import thing that you've missed is that there is almost no environment set up for cron jobs. Especially, there is no $PATH set up, so you either need to set one up at the start of the script, or you need to explicitly use the path with every command - eg /bin/ls "

 

Answer: Steve is correct, so as Ubuntu describes in their documentation about Crontab (but wasn't mentioned here):

  1. "It is recommended that you use the full path to the desired commands as shown in the above examples."
  2. "Depending on the commands being run, you may need to expand the root users PATH variable by putting the following line at the top of your crontab file:
PATH=/usr/sbin:/usr/bin:/sbin:/bin
Article was posted on Sunday 14th of August 2011 @ 17:37 PM CEST   comment(s)

 TVheadend Forum Migration Notice

XML error: Invalid document end at line 37