PDA

View Full Version : Cron: A beginners guide to scheduled tasks,Linux Style!


darksidepuffin
03-24-2005, 07:07 PM
Cron: A beginners guide to scheduled tasks,Linux Style!


By: William Crooks <http://www.avianhosting.com>


This tutorial may NOT be copied without permission



Crontab is a feature of Linux servers which allows you to execute a specified command at set intervals. The intervals can be any valid combination of the arguments specified as the first five entrys in a crontab line. These entries are as follows:

minute: Specify the 'minute'(usually used in conjunction with 'hour') at which the command executes.


30 * * * * cron.sh


Would execute 'cron.sh' at the 30th minute every hour, every day of the week, every single month...etc. Meaning that everyday cron.sh will run 24 times!If you have something to run 24 times a day...you really..really need to rethink the process.

hour: The hour at which to run the command. This is specified as a number between 0 and 23. Yeah..most people are used to standard 12 hour time,right?Which is why the following list provides the translations of 12 hour time to cron-style 23 hour time:

12 Hour Time Cron Time
12 midnight - 0
1 am - 1
2 am - 2
3 am - 3
4 am - 4
5 am - 5
6 am - 6
7 am - 7
8 am - 8
9 am - 9
10 am - 10
11 am - 11
12 noon - 12
1 pm - 13
2 pm - 14
3 pm - 15
4 pm - 16
5 pm - 17
6 pm - 18
7 pm - 19
8 pm - 20
9 pm - 21
10 pm - 22
11 pm - 23



Not too complicated, right?

So...


30 0 * * * cron.sh


Would run the script at 12:30(eg: half past midnight) everyday. Not a bad timing for a re-cache script or other intensive yet non-end user involving process. Bear in mind that all times are the server's local time.

Day Of Month: Standard 1-31 day of the month. I doubt this requires much explaination, so I'll get straight to an example:


30 0 5 * * cron.sh


Executes our previous script at 12:30 on the 5th of the month, every month. Possible usage of a once a month cron job would be, for example, a log rotator.

Month: Standard 1-12 numerical indicator of the month. I doubt this requires much explaination, so I'll get straight to an example:


30 0 5 2 * cron.sh


Executes our previous script at 12:30 on the 5th of February, every February(eg: once a year).

Day Of Week: A number, between 1 and 7, which indicates the day of the week to execute the command on. It starts with monday, which is the number 1 for this argument.


30 0 * * 1 cron.sh


Executes cron.sh at 12:30(eg: half past midnight), every monday. Thus this command would be executed once a week.

Command Line Cron

Using cron on the command line can be very effective, but in order to do so you must understand the crontab function, and have appropriate access to use it(ask your host). In addition, most servers will have a system-wide crontab file (usually located at /etc/crontab).

Common Arguments to crontab

Argument Functionality

e - Edit your cron file
l - View your cron file
r - Remove your cron file


Using:


crontab -e


will open your crontab file for editing. When opened...it will display output similar to the following:


30 5 * * * random_command_here > possible_extra_command

45 1 * * * random_command_here > possible_extra_command

....


of course those are just examples, but you will see(unless, of course, the file is empty) 6 clearly tabbed sections in the crontab command list. You can modify/remove these, but we will focus on adding a new one. On a blank line in your crontab file, add the 'tab' you wish to execute(for example purposes we will use one from the above explainations) below all of the others:


30 0 * * 1 cron.sh


ensuring to maintain appropriate spacing(which should be made with the [tab] button on your keyboard).

To view your crontab file, use the following command:


crontab -l


this will provide output similar to:


30 5 * * * random_command_here > possible_extra_command

45 1 * * * random_command_here > possible_extra_command

30 0 * * 1 cron.sh


I won't cover using crontab -r here, since it's usage should be straightforeward.

Additional Functionality

In the above examples, you will notice that there is a


> possible_extra_command


after the command part of the crontab line. One functionality of this is to enable you to log the results of the command (useful if, for example, your command modifys/moves/otherwise works with the files on the server). To log the result of a command, add the following to the end of the crontab line:


> /path/to/log/file.log


So in keeping with our previous crontab example:


30 0 * * 1 cron.sh > /path/to/log/file.log


would log the results of the execution of cron.sh to /path/to/log/file.log.

Crontab also has a useful feature in that it e-mails the user with the results of the crontab execution. However, this is not always needed, so to remove it use:


>/dev/null 2>&1


or...example:


30 0 * * 1 cron.sh >/dev/null 2>&1


would execute cron.sh without e-mailing you. In addition, using crontab in the form of:


crontab /path/to/a/crontab


which will execute the crontab file specified in the argument.

Cron and CPanel

CPanel contains in itself an online cron job editor.To access this(assuming your using the "x" skin):
Login to your CPanel( yourdomain.com/cpanel )
Scroll down to near the bottom of the menu items
Click the 'Cron Jobs' Icon
Select 'Advanced (Unix Style)' (I just taught you how to do it, the least you can do is use that knowledge..hehe)


Cron and Red Hat Linux

In addition to the standard ``crontab'' entries, Red Hat adds several directories:

/etc/cron.hourly/
/etc/cron.daily/
/etc/cron.weekly/

As their names suggest, executable files can be placed in any of these directories, and will be executed on an hourly, daily, or weekly basis. This saves a bit of time when setting up frequent tasks; just place the executable script or program (or a symbolic link to one stores elsewhere) in the appropriate directory and forget about it.

This tutorial may be expanded upon in the future, but for now this should at least give you an introduction to the power of crontab, meanwhile clearing up some of the initial fright of it's often imposing appearance.

Credits/Changelog
-Updated based on suggestion by Crouse
-Updated based on suggestion by AaronCampbell

Erikina
03-29-2005, 05:12 PM
Very nice tutorial! Now I don't need to use the "standard" cron thingy in Cpanel :D

Without luck, I'm trying to run a cron to open a php file on a different server, by giving it a direct URL like:

* 0 * * * http://www.server.com/update.php

Is this possible to do?

Thanks a lot.

darksidepuffin
03-29-2005, 05:52 PM
In all honesty I haven't tried that...but I don't believe so -- that could be very dangerous if it was allowed. I would say it would need to be a local path.

And..for a php file(again...the tutorial was written from knowledge...not something I've done a lot, tho) I believe you'll need to make sure the server knows to use php to execute it...eg:


* 0 * * * php update.php


it may work without, it may not...but I'm pretty near positive it won't work with a url.

McDude
05-07-2005, 04:13 AM
Should this not be in the Server Administration forum? I think you posted this before the forums were created.

darksidepuffin
05-07-2005, 09:21 AM
Yes I did..I was about to post a link to this in your crontabs thread :rolleyes:

Crouse
05-17-2005, 01:35 AM
Nice tutorial.....

I would like to add.....
--------------------------------------
In addition to the standard ``crontab'' entries, Red Hat adds several directories:

/etc/cron.hourly/
/etc/cron.daily/
/etc/cron.weekly/

As their names suggest, executable files can be placed in any of these directories, and will be executed on an hourly, daily, or weekly basis. This saves a bit of time when setting up frequent tasks; just place the executable script or program (or a symbolic link to one stores elsewhere) in the appropriate directory and forget about it.
--------------------------------------
http://www.tldp.org/LDP/lame/LAME/linux-admin-made-easy/using-cron.html

darksidepuffin
05-17-2005, 06:17 AM
Good point Crouse. I neglected to mension that (certainally not something I shouldeve foregot having spent most of my admin time on Red Hat systems :rolleyes: ) -- addendum will be applied.

AaronCampbell
05-17-2005, 06:28 PM
/etc/cron.hourly/
/etc/cron.daily/
/etc/cron.weekly/I'd like to add that you can make your own directories like this. Most distros include a file called 'run-parts', but if your distro does not, google it, or try this one:#!/bin/bash
# run-parts - concept taken from Debian
# keep going when something fails
set +e
if [ $# -lt 1 ]; then
echo "Usage: run-parts <dir>"
exit 1
fi

if [ ! -d $1 ]; then
echo "Not a directory: $1"
exit 1
fi

# Ignore *~ and *, scripts
for i in $1/*[^~,] ; do
[ -d $i ] && continue
# Don't run *.{rpmsave,rpmorig,rpmnew,swp} scripts
[ "${i%.rpmsave}" != "${i}" ] && continue
[ "${i%.rpmorig}" != "${i}" ] && continue
[ "${i%.rpmnew}" != "${i}" ] && continue
[ "${i%.swp}" != "${i}" ] && continue
[ "${i%,v}" != "${i}" ] && continue

if [ -x $i ]; then
$i 2>&1 | awk -v "progname=$i" \
'progname {
print progname ":\n"
progname="";
}
{ print; }'
fi
done
exit 0Then, make a directory. Name it whatever you want, put it wherver you want. Then put a line in your crontab like this:* * * * * YourUsername run-parts /path/to/YourDir(make sure to make the obvious replacements, and to NOT use * * * * * unless you are SURE you need it.

AaronCampbell
05-17-2005, 06:33 PM
Also note, that there is usually a default system-wide crontab file at /etc/crontab and it can be edited with any text editor (such as vi). Also, you can change what crontab file you use with: "crontab /path/to/your/crontab" and if you are the system administrator, you can easily use the system-wide crontab with "crontab /etc/crontab"

darksidepuffin
05-18-2005, 06:53 AM
And they say constructive criticism is dead :rolleyes: you guys just pointed out two extreme omissions on my part...without ripping me up about it ^_~