Cron
From TextUsers
cron is the name of program that enables unix users to execute commands or scripts (groups of commands) automatically at a specified time/date. cron is a daemon, which means that it only needs to be started once, and will lay dormant until it is required. A Web server, for example, is also a daemon; it stays dormant until it gets asked for a web page. The cron daemon, or crond, stays dormant until a time specified in one of the config files, or crontabs. How to start cron: The good thing is you don't need to worry about starting cron, as the good people at TxD do it for you.
Contents |
Using cron
You can edit a crontab (the name for cron's config files). The main config file is normally /etc/crontab at root level, and you can not see it directly. At any TextDrive shared account, a crontab would look something like this:
01 * * * * run-parts /etc/cron.hourly 02 4 * * * run-parts /etc/cron.daily 22 4 * * 0 run-parts /etc/cron.weekly 42 4 1 * * run-parts /etc/cron.monthly
An entry in cron is made up of a series of fields separated by a space. There are normally six fields in one entry.
- minute - minute of the hour the command will run on, between '0' and '59'
- hour - hour the command will run on, between 0 and 23 (0 is midnight)
- dom - Day of Month the command will run on, e.g. to run a command on the 19th of each month, the dom would be 19.
- month - month a specified command will run on, (01-12) or as the name of the month (e.g. May)
- dow - Day of Week a command to be run on, between 0 and 7 (0 and 7 are both Sunday), or as the name of the day (e.g. sun).
- cmd - command that you want run. This field may contain multiple words or spaces.
If you don't wish to specify a value for a field, just place a * in the field.
01 * * * * echo "This command is run at one min past every hour" 17 8 * * * echo "This command is run daily at 8:17 am" 17 20 * * * echo "This command is run daily at 8:17 pm" 00 4 * * 0 echo "This command is run at 4 am every Sunday" * 4 * * Sun echo "So is this" 42 4 1 * * echo "This command is run 4:42 am every 1st of the month" 01 * 19 07 * echo "This command is run hourly on the 19th of July"
Notes: Under dow 0 and 7 are both Sunday. If both the dom and dow are specified, the command will be executed when either of the events happen.
* 12 16 * Mon cmd
Will run cmd at midday every Monday and every 16th, and will produce the same result as both of these entries put together would:
* 12 16 * * cmd * 12 * * Mon cmd
Vixie Cron also accepts lists in the fields. Lists can be in the form, 1,2,3 (meaning 1 and 2 and 3) or 1-3 (also meaning 1 and 2 and 3).
59 11 * * 1,2,3,4,5 backup.sh
Will run backup.sh at 11:59 Monday, Tuesday, Wednesday, Thursday and Friday, as will:
59 11 * * 1-5 backup.sh
Cron also supports 'step' values.
A value of */2 in the dom field would mean the command runs every two days and likewise, */5 in the hours field would mean the command runs every 5 hours.
* 12 10-16/2 * * backup.sh
is the same as:
* 12 10,12,14,16 * * backup.sh
*/15 9-17 * * * connection.test
Will run connection.test every 15 mins between the hours or 9am and 5pm
Lists can also be combined with each other, or with steps:
* 12 1-15,17,20-25 * * cmd
Will run cmd every midday between the 1st and the 15th as well as the 20th and 25th (inclusive) and also on the 17th of every month.
* 12 10-16/2 * * backup.sh
is the same as:
* 12 10,12,14,16 * * backup.sh
When using the names of weekdays or months, it isn't case sensitive, but only the first three letters should be used, e.g. Mon, sun or Mar, jul. Comments are allowed in crontabs, but they must be preceded with a '#', and must be on a line by them self.
Controlling Access to cron
Cron has a built in feature of allowing you to specify who may, and who may not use it. It does this by the use of /etc/cron.allow and /etc/cron.deny files. These files work the same way as the allow/deny files for other daemons do. To stop a user using cron, just put their name in cron.deny, to allow a user put their name in the cron.allow. If you wanted to prevent all users from using cron, you could add the line ALL to the cron.deny file:
root@pingu # echo ALL >>/etc/cron.deny
If you want user cog to be able to use cron, you would add the line cog to the cron.allow file:
root@pingu # echo cog >>/etc/cron.allow
If there is neither a cron.allow nor a cron.deny file, then the use of cron is unrestricted (i.e. every user can use it). If you were to put the name of some users into the cron.allow file, without creating a cron.deny file, it would have the same effect as creating a cron.deny file with ALL in it. This means that any subsequent users that require cron access should be put in to the cron.allow file.
Output from cron
The output from cron gets mailed to the owner of the process, or the person specified in the MAILTO variable. If you want to mail the output to someone else, you can just pipe the output to the command mail.
cmd | mail -s "Subject of mail" user
Substitute user for the email address of the person who wishes to receive the output.
If you have a command that is run often, and you don't want to be emailed the output every time, you can redirect the output to a log file (or /dev/null, if you really don't want the output).
cmd >> log.file
Notice we're using two > signs so that the output appends the log file and doesn't clobber previous output. The above example only redirects the standard output, not the standard error, if you want all output stored in the log file, this should do the trick:
cmd >> logfile 2>&1
You can then set up a cron job that mails you the contents of the file at specified time intervals, using the cmd:
mail -s "logfile for cmd" <log.file
crontab
Editing file or crontab by shell commands is challenging and required proficiency in vi editor.
1. ssh into your account (shell commands)
2. Create a file, the name does not really matter. I will call mine mycrons.cron.
3. Here is the format of the cron jobs you will schedule:
# Minute Hour DOM Month DOW */45 * * * * /bin/echo "This cron job is scheduled to occur every 45 minutes" * 11 * * 2 /bin/echo "This will go off at noon-time every tuesday"
Note: You must put the full path to the application you want to execute. To find out where, for example ruby is located, type "which ruby", and this will give you the path to the ruby application.
4. Now you must add mycrons.cron to the crontab. To do this, "crontab mycrons.cron".
5. Check if your cron jobs where added, "crontab -l".
6. If you need to edit a cron job, never edit mycrons.cron. Instead, edit it with "crontab -e".
7. To remove mycrons.cron from your crontab, issue "crontab -r".
