Wednesday, March 17, 2010

Basics of Cron Jobs

Cron is the name of the program that enables *nix users to execute commands or scripts automatically at a specified time/date. It is normally used by sys admins, to run back-ups or do other repetitive tasks like the command makewhatis, which builds a search database for the man -k command. However it can be used for anything. A common use for it today is connecting to the internet and downloading your email.

For today's example, which will be used to teach you the basics of what a cron job does, I will use the command:
rm /home/username/htdocs/*~

This command tells the machine to delete all of my backup files. Generally, *nix-based machines put a ~ after the file name to denote a backup. For example, if my machine froze while I was editing a file named work.php, the system would save a file named work.php~. If I didn't notice this, and work.php had database configuration information in it which would include my database name and password, then a hacker may be able to access the source of work.php by going to mysite.com/work.php~. Obviously, I don't want my passwords floating around, or source for my code, in some cases. Note: * is a wildcard in *nix, so I'm saying delete everything that ends in ~.

The basic cron job has 2 or 3 parts: date/time, command, and the log file [optional]. The date/time is formatted so that you can tell the machine how often to perform the task in a simple format:
minute hour day/month month day/week

For the day of week, 1 is monday, and therefore 0 is sunday. However, 7 is also considered sunday. Also, the cron job will execute when either day/month or day/week is true. Here is a common diagram of the fields that I used alot when I was new to cron jobs:

* * * * * 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)[/code]

The command for editing cronjobs is:
crontab -e

other parameters for crontab are:
crontab -l = display cron file
crontab -r = remove cron file
crontab -v = display last time crontab file was edited (not universally available)

Go ahead and type that first command, the one with the -e parameter. If you don't know vi, or you are pretty confused as to what just came up. I recommend learning vi, it's awesome, but in the mean time, just go ahead and type:
i

This should let you enter inset mode. Now, type:
* * * * * rm /home/username/htdocs/*~

then press the Esc key and then type:
:x

Congrads your cronjob has been installed! It will now delete all backup files in your htdocs folder every minute.

You can also make log files. Go ahead and execute this command in your home folder:
mkdir cronlogs

Now, go back into your cron file like you did above and add the Following after the command:
> /home/username/remove_backups.log

your cronjob will now look like:
* * * * * rm /home/username/htdocs/*~ > /home/username/remove_backups.log

There we go! You have now made your first cronjob. However there is another way to use cron jobs. In the /etc directory you will probably find some sub directories called 'cron.hourly', 'cron.daily', 'cron.weekly' and 'cron.monthly'. If you place a script into one of those directories it will be run either hourly, daily, weekly, or monthly, depending on the name of the directory.

Thanks for reading, hope this helps people new to *nix.