Script Tips

Monday, 16 February 2009 16:50

Cron Jobs

Written by 
Rate this item
(0 votes)

Cron Jobs

Linux has a very nice tool for those that want to have a scheduled action of some sort. The cron job allows you to set a script or command to run on a schedule that you set.

The syntax for the command looks like this:
0 9 * * * * /home/mysite/cgi-bin/somescript.cgi

You may want it to delete files every so often in a folder that has cache or other files that build up.

You can run PHP scripts or in some cases even use Curl to help out.
Curl is a software that allows you to access your website or some other website by Curl calls.
So if you use Curl in a cron job it would look like this:
0 9 * * * *  curl

Why use Curl? In some cases you may need to. Maybe due to some dynamic reasons or what ever reason which requires a  Php or a Perl script to only work when called from your website URL. Curl in this case maybe the answer and it allows you to pass parameters either as a get or post.

Getting back to Cron Jobs we can set them up very easy and it's quite simple.
There are 6 numbers or wild card symbols separated by a space.
* is a wild card which means every day, hour depending on which place it's in.

The entry places are setup as follows:
minute  hour   day   month   day of week

Each is a number or wild card where a asterisk ( * ) is the wild card.
Minutes are from 0 to 59
Hours are from 0 to 23
Day is from 1 to 31
Month is from 1 to 12
Day of Week is from 0 to 6

Hours are in 24 hour format so 13 would be 1 PM
Zero = 12 AM
For day of the week zero equals Sunday.

0 * * * * *  This would mean it fires every hour on the hour.
1 5 * * * *    The first minute of the 5th hour of every day
0 0 1 1 *    The first hour of the first day of the first month  or once per year.

We also have the slash, forward slash to be more precise to have it do the same task every 2 minutes, hours, days, months or weeks.

 * */3 * * *  Here we set it for every third hour.
* */2 * * *  Every other hour.

If you have a shared hosting account most Control Panels have a feature to set a cron job. For those that are not you will need to have access by using Shell.
For those that use Shell you will need to run crontab.
To view cron jobs you run crontab -l and to edit or add cron jobs use crontab -e

Hope this helps.
Read 8185 times Last modified on Saturday, 25 April 2015 20:45

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.