Entry
How do I set up a cron job on Linux?
Jun 3rd, 2001 12:00
Philip Olson, Aaron Young, Ben Munoz, Nathan Wallace,
The easiest way for a user to set up a cron job (this is pretty
standard) is to execute :
crontab -e
This will open up an editor (mine is vim) and then you can edit your
personal crontab file. To view your users default text editor, try the
following (in shell) :
echo $EDITOR (or perhaps $VISUAL)
To modify the default editor, modify this setting. Have a look at a
file similar to .profile (or .bashrc ...) and note and modify the
following :
EDITOR=vim;
But this has nothing to do with crontab, we digress ;-) Moving on to
crontab, consider the following :
A crontab file has 5 specifiers and a string all separated by spaces.
The specifiers corresponding to the date/time you want your job to run
and the string being what is executed when the specified time is met.
An example always helps so let's say we want to schedule a simple email
to me@me.com every 30 minutes between the hours of noon and 1pm, on the
first day of the month, in April, if it happens to be a Wednesday. The
steps I would take are
First, simply type the following into shell :
crontab -e
(note: an editor will automagically pop up)
Press Enter
Then type the following :
00,30 12,13,14 1 4 3 echo "you fool!" | /usr/lib/sendmail me@me.com
Now for a breakdown of what we've done :
00,30 : This is where you specify the minutes (0-59) We have
chosen both :00 and :30 (right on the hour and
half hour)
12,13,14 : These are the hours, it is in military time so 0-23
(this example equals to 12pm, 1pm, and 2pm)
1 : The day of the month (1-31) This is of course the first
day of the month.
4 : This is the month (1-12) April in this case
3 : This is the day of the week (0-6 with 0 being Sunday)
Wednesday is being used in example.
Everything else on the line is the command. In the example we're
echoing "you fool!" into an email that's being sent to me@me.com We
chose /usr/lib/sendmail as that's the path of sendmail, this can be
found out using the 'which command' so in shell do 'which sendmail' and
it'll give you the path to your sendmail.
This description is useful for a quickstart, but you really want to
read through the man page (man crontab), since there are other things
like whether or not the user in question can even use cron that aren't
covered here.
A few notes :
The numbers can be replaced with an asterisk (*) to mean that it
happens all the time, so if I wanted to be notified every hour on every
April Fool's day :
0,30 * 1 4 * echo "How foolish" | /usr/lib/sendmail me@localhost
See I replaced the 12,13,14 with and * so that it will happen every
hour and the 3 (for Wednesday) with a * to make it every day of the
week.
A dash (-) can be used to denote spanning. So, let's say we want to
get an email Monday through Friday to remind us to eat lunch, this can
be done as such :
0 12 * * 1-5 echo "Eat" | /usr/lib/sendmail me@localhost
Which would spit out an email to us every weekday at Noon. We use
me@localhost as localhost denotes the server and me being the username
on that server ... just another way to do it.
If you don't have access to cron, consult your local admin. And
remember, in shell you can access the cron manual by doing :
man cron
Or
man crontab