Recent Changes - Search:

edit SideBar

Cron

I wanted to set up a computer to do some tasks automatically on a regular basis. It took some wading through the online stuff I found. However, the very basics could be explained relatively easily I thought.

Commands crontab -e - Allows you to edit the crontab. If there is not already a crontab one is created.

crontab -l - Shows you what is already in your crontab. There can be a crontab for every user including root.

crontab -r - Removes the crontab. You can just delete everything out of the crontab using crontab -e and then using delete on the keyboard and that seems to get rid of it too.

Format for entries The basic format is having 5 columns and then the command that is to be run. The first column indicates the minutes, second the hour, third the day, fourth the month and fifth is the day of the week. The choice of entry for all these columns is * means is it to happen every time i.e. every month or every day, or a number indicates the when you want it to happen i.e. a 10 in the minutes column means that the instruction is to take place at 10 minutes past the hour.

Note that a 0 in the hour column means midnight and 0 in the day of the week column is Sunday.

If you want to try it out do the following:

As your normal user type crontab -e and a text editor will open with nothing in it (unless you or someone else has aready set something up). Type as below:

10    *    *    *     *    date    >>   /home/REPLACE_WITH_YOUR_USER_NAME/testing_cron.txt 
 This will create and then add to a file called testing_cron.txt. The curent date (at the time it is run) will be appended to the file.

You can set it up to do things several hours in the day by putting commas and another time. The example below will add the current time to your testingcron.txt at 3:10am 6:10am and 9:10am each day.

10     3,6,9     *     *    *    date   >>   /home/REPLACE_WITH_YOUR_USER_NAME/testing_cron.txt

Additionally if you want something to happen say every 15 minutes you can use the / to indicate this, for example:

*/15   *    *    *    *    date    >>   /home/REPLACE_WITH_YOUR_USER_NAME/testing_cron.txt 

In a slight variation to this, if you want every 15 minutes on a Sunday between 2pm and 6pm:

*/15   14-18    *    *    0    date    >>   /home/REPLACE_WITH_YOUR_USER_NAME/testing_cron.txt 

(In this example, */15 = every time the minute is evenly divisible by 15, 14-18 = 2pm - 6pm, and 0 = Sunday

A couple of things to watch out for:

  • Make sure that you hit enter at the end of the line.
  • If you don't care about the output of the command (i.e. whether it works or not) you can suppress all errors from the command by putting '> /dev/null 2>&1' at the end of the command. If your cron does output something when it runs (e.g. it lists the contents of a directory) then that content will be mailed to you (note: sent to your account on your computer, not your ISP account).
Edit - History - Print - Recent Changes - Search
Page last modified on October 09, 2009, at 09:33 AM