Linux:software:cron
From Linux How-To Repository
Contents |
About Crontab
Sources: http://www.adminschoice.com/docs/crontab.htm and http://ubuntuforums.org/showthread.php?t=102626
Crontab is a program that can run tasks for you automatically at regular intervals using the cron daemon. These tasks are often termed as cron jobs. The system maintains a crontab for each user on the system. Everything is setup in a simple text file.
You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use crontab if your name does not appear in the file /usr/lib/cron/cron.deny. If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line. Often people just use root to handle all cron jobs.
I've been using cron.daily, cron.weekly and cron.monthly in the /etc directory to run scripts regularly. Everything is taken care of with those directories. So, I had no really cause for manually setting up cron jobs, but when I did want to run them, I thought it was going to be complicated. Once I looked into it, though, I found it was surprisingly easy.
Crontab Commands
Here are the only commands you will need:
crontab -e Edit your crontab file, or create one if it doesn't already exist. crontab -l Display your crontab file. crontab -r Remove your crontab file. crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)
To create a crontab entry simply type this:
crontab -e
If you want a particular editor to handle this task you would do this first:
export EDITOR=gedit
Or this in one line:
EDITOR=gedit && crontab -e
Of course, if you have a default editor setup in your .bashrc file, you don't have to worry.
To create a crontab entry for root to handle use sudo:
sudo crontab -e
Careful you don't get things confused between root's and your user crontab.
Other Editing Options
GUI tools exist for editing crontab, like Gnome Schedule (not in the repositories) or KCron (available on the repositories).
The Crontab File
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval. It's laid out like this:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command
Except that you don't use commas after each time reference, only within each one. Here is another layout:
* * * * * 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)
The * in the time reference fields above means all legal values apply. The time reference field can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).
Note: The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, both entries will get executed .
Basic Crontab Examples
A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 pm.
30 18 * * * rm /home/someuser/tmp/*
Changing the time reference values as below will cause this command to run at different time schedules:
min hour day/month month day/week Execution time 30 0 1 1,6,12 * -- 00:30 Hrs on 1st of Jan, June & Dec. 0 20 * 10 1-5 -- 8.00 PM every weekday (Mon-Fri) only in Oct. 0 0 1,10,15 * * -- midnight on 1st ,10th & 15th of month 5,10 0 10 * 1 -- At 12.05,12.10 every Monday & on 10th of every month
The crontab will begin running as soon as it is properly edited and saved.
Crontab Environment & Extras
cron invokes the command from the user's HOME directory with the shell, (/usr/bin/sh).
cron supplies a default environment for every shell, defining:
HOME=user's-home-directory LOGNAME=user's-login-id PATH=/usr/bin:/usr/sbin:. SHELL=/usr/bin/sh
Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.
Disable Email
By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .
>/dev/null 2>&1
Of course, you might want to know that a cron job has run via another email address. For that you could just put a mutt command at the end of any script you run.
Generate a Log File
To collect the cron execution execution log in a file:
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log
This is using the example employed earlier.
Stopping & Starting Cron
Debian or Ubuntu Linux the following commands. Task: Start corn service
To start the cron service, use this:
# /etc/init.d/cron start
Or
$ sudo /etc/init.d/cron start
To stop the cron service, use this:
# /etc/init.d/cron stop
Or
$ sudo /etc/init.d/cron stop
To restart the cron service, use this:
# /etc/init.d/cron restart
Or
$ sudo /etc/init.d/cron restart
More Cron Job Examples
Round-the-Clock Intervals
One usage that's very helpful is this:
0 */x * * * command,
In this one, x is a digit you nominate and will repeat the command every x hour. Here is an example:
*/5 * * * * /path/to/a/script
In the above example, the script will run every 5 minutes, 24 hours a day, all year round.
This kind of entry can also be done with ranges:
0 8-20/4 * * * command
That would execute command every 4 hours between 8 am and 8 pm
More Complicated Intervals
0 0 1,15 * 1 command
The above entry would run the command or script at midnight on the first and fifteenth of each month, as well as every Monday; that is, it runs if it's the 1st, 15th, OR any Monday.
01 04 1 1 1 /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on any Monday that falls on January 1st.
01 04 * * * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on every day of every month.
Comma-seperated values can be used to run more than one instance of a particular command within a time period. Dash-seperated values can be used to run a command continuously.
01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 01 and 31 past the hours of 4:00 am and 5:00 am on the 1st through the 15th of every January and June.
45 04 * * * /usr/sbin/chkrootkit && /usr/bin/updatedb
The above example will run chkrootkit and update the slocate database every morning at 4:45 am.
Using Cron to Run Applications at a Set Time
To run a GUI command on cron, you'll have to give cron what display the program should use. For that you use:
export DISPLAY=:0
- 0 is the default. If you like the program to run on some other display, change the number. Thus your final crontab entry would look like this:
01 04 * * * export DISPLAY=:0 && /usr/bin/somedirectory/somecommand
Let's say you want amarok to play the radio station antena2 at a specific time:
30 16 * * 7 export DISPLAY=:0 && amarok mms://rdp.oninet.pt/antena2
This will make amarok start the Antena2 url every Sunday, at 16:30. Then, if you to close amarok after a certain period, you would use this:
0 17 * * 7 export DISPLAY=:0 && amarok -s
This will cause amarok to exit at 17:00 every Sunday.
You can use this export trick with any application.
Other programs can be exited with killall:
0 9 * * * killall nicotine
This will shut nicotine down at 9 am.
Troubleshooting
When I wanted to keep certain cron scripts in a separate directory, I found they weren't running. I think the problem was that they belonged to a different user or else were not executable. Make sure any scripts you run have root privileges.
