Author Topic: Running a CRON Job  (Read 4267 times)

Offline Abool

  • Newbie
  • *
  • Posts: 29
Running a CRON Job
« on: December 29, 2011, 08:53:23 PM »
Using cron by DJG

On Unix systems, cron is used for automating tasks. Instead of getting out of bed at 3 A.M. to run a command that can be anything from backing up a drive, or a command that deletes old files, you can use cron to schedule those tasks.

There is no actually program called "cron". There are however, the program "crontab" and the daemon "crond". By running the "crontab" program, you can enter commands in a text file, and they will automatically be saved in the /var/spool/cron/ directory. For example my entries (for user djg) would be in /var/spool/cron/djg. The crond daemon reads the files regularly and executes the commands at the time they are scheduled for.

There are many different uses, from checking your broadband connection overnight to maintain a stream or hefty download, or simply checking up to see what's going on in your digitised social space. Let's run with that second one as a working example. Say for some reason, you want to check the output of the command 'w' every hour to see who's on.

The setup of a crontab file is as follows:

There are six fields:

1 minute (0-59)
2 hour (0-23)
3 day of month (1-31)
4 month (1-12, or name such as jan, feb, etc)
5 day of week ( 0-6(6 = Sunday) or name such as mon, tue,etc)
6 command to run

So an example could be this:

0 1 24 5 0 w

That will run a command at 1:00AM on Monday, May 24th. Now that gets a bit cyptic. To make it a little better, this would also work:

0 1 24 may mon w

But what if you want it to run every hour, regardless of date? An "*" means that that field doesn't matter, or do the command no matter what is in those fields. So to run our 'w' command every hour, the command would be this:

0 * * * * w

Which means that it runs everyday, every hour at the 0 minute mark, meaning the beginning of the hour.

A bunch of different variations of fields can be generated like this. For instance, say you wanted a command to run every 2 hours. You could specify the "hour" field as this: */2 Which would run at 2,4,6,8, etc...

You can also use commas to specify more than one time. For instance, say you want to run it at half past the hour, and a quarter of. You could specify the minute field as this: 30,45

If you use a dash between two values, it will include everything in between them. An example of this would be to run a command everyday for the first week of a month. The day of the month field would be this: 1-7

So to have the commmand run every 2 hours, at half past and a quarter of, and run for the first 7 days of a month. We would have this:

30,45 */2 1-7 * * w

To save the output to a text file, such as 'wholog' you could write the command like this:

0 * * * * w >> /home/djg/wholog

Now by default, you will be emailed the output as well, so to avoid this, add this to the command:

0 * * * * w >> /home/djg/wholog 2>&1

Also, if you didn't want any output at all you could redirect all output to /www.null like this:

0 * * * * w >> /www.null 2>&1

Now... that command is pretty useless since you don't get any output, but for some, like if your program performs a task such as checks to see if your CD is mounted and if so, unmounts it, or something that doesn't really require output, then it may be useful.

Now you may be wondering how you actually use crontab to get crond to run your command. To create or edit your cron entries, use the command 'crontab -e' this will open a text editor and then you can add cron entries to your hearts content, each on one line.

The command 'crontab -e' will run vi by default. If you don't want that, change your VISUAL enviornment variable. I have set up to run mine as pico with wordwrapping turned off (using the -w flag) You can do this in bash like this:

export VISUAL='pico -w'

Ok. So you've made a bunch of crontab entries and now want to see all the commands you've entered. You can use the command 'crontab -l' to list all of your crontab entries.

You can edit your crontab file and remove entries if you'd like, but if you want to nuke them all, use the command 'crontab -r'

Note: root can edit other users crontabs like this: 'crontab -e -u username'

A nice little trick is to setup a directory with a bunch of scripts you want run at a certain time, such as every hour, then make this entry in your crontab:

0 * * * * run-parts /home/djg/hour.cron/

Or something to that effect. That command will run everything in the /home/djg/hour.cron/ directory. /etc/crontab shows an example of this so you can check that out to see it in action.

Other Related Sites:

Suggest a link?:
mail us

Linux Hosting by


Offline Abool

  • Newbie
  • *
  • Posts: 29
Re: Running a CRON Job
« Reply #1 on: December 29, 2011, 09:09:09 PM »