Our Friendly Daemon: CRON
Do you have jobs that you could automate? Perhaps a program or a script that needs running at a certain time of day, or maybe a backup to kick off at 2am?
Rather than using some third party application, or setting an alarm to remind you to do it, or waiting up until 2am every night, use CRON, it's already part of your system as long you're using UNIX or Linux based systems.
The way I've been introduced to CRON was through the command line, and that is what I'll be showing you first, then we'll look at how you can use a GUI if you're not comfortable with the CLI.
Command Line CRON
OK, fire up your command line application (Terminal, xterm, gnome-terminal, etc) and enter the following:
$ crontab -e
Don't forget, "$" doesn't need to be entered. The '-e' is to edit the CRON table. You should find a blank screen, you have been launched into your default text editor.
*BEGIN SIDE NOTE*
Your default editor is set using the EDITOR variable (in bash), change it for the current session using this:
$ export EDITOR=
Replace with an installed program, for example: vi, vim, emacs, nano, pico, etc.
To make this more permanent, add the line above to your .bash_rc
*END SIDE NOTE*
OK, so you have the default editor up, now enter input mode (press 'i' for vi(m)).
CRON takes arguments that are separated by a space, it is important that you don't use tabs! The arguments are like this:
minute hour day_of_month month day_of_week command
*Values*
Minute = 0-59
Hour = 0-23
Day_of_month = 1-31
Month = 1-12
Day_of_week = 0-6 (Sunday == 0)
So, the editor is up, we know what the values are, lets do some examples:
# Add comments with a hash!
# OK, for that 2am backup:
58 1 * * * /opt/scripts/user-area-backup
#Lets do a whole system backup once a month:
43 22 1 * * /opt/scripts/full-system-backup
#Just for fun, let's crash the system on the seventh day of the tenth month at 5am, but only when that day is a Friday
0 5 * 7 10 5 /opt/scripts/crash
#Last one
#Lets purge a temporary folder every two hours, between 8am and 8pm, Monday to Friday:
0 8,10,12,14,16,18,20 * * 1,2,3,4,5 /opt/scripts/remove_temp_files
As you can see, I've used asterisks to say "every", and commas to list multiple values for an argument.
Easy stuff I hope you'll agree, in fact that hardest things you have to do in the above, is make the scripts and put them somewhere sensible!
One last thing to note, you need to add a blank line at the end of the file! If you're using Vi or Vim then it is done for you.
GUI CRON
There are a number of GUI editors that will do the job for you, having done some research, and a little playing around, I've found the following:
- CronniX for OS X ( http://h775982.serverkompetenz.net:9080/abstracture_public/projects-en/cronnix ) gives you a simple and expert interface, it does have an interface for intervals, but it is not active as of version 3.2
- Grontab for Linux/UNIX ( http://www.arquired.es/users/aldelgado/proy/gcrontab/ ) provides a simple GUI for managing CRON tasks
- The GNOME Task Scheduler (Gat) ( http://piki.org/patrick/gat/ ) is another GUI for Linux/UNIX which provides basic functionality.
And that's that, I hope that'll help some of you schedule important scripts. I'll look at writing another article on the `at` command, which will run a command once. I've a feeling that CRON handles `at` jobs too.
| Next > |
|---|
Last Updated (Sunday, 26 December 2010 08:51)



