0

Cron Job – Managing cron tab

Posted by admin on Nov 13, 2009 in IT, PHP

Cron is a time-based job scheduler in Unix-like computer operating systems. ‘cron’ is short for Chronograph.

Cron enables users to schedule jobs (commands or shell scripts) to run automatically at a certain time or date. It is commonly used to perform system maintenance or administration, though its general purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.

With the advent of the GNU Project and Linux, new crons appeared. The most prevalent of these is the Vixie cron, originally coded by Paul Vixie in 1987. Version 3 of Vixie cron was released in late 1993. Version 4.1 was renamed to ISC Cron and was released in January 2004. Version 3, with some minor bugfixes, is used in most distributions of Linux and BSDs.

In 2007, RedHat forked vixie-cron 4.1 to the cronie project and included anacron 2.3 in 2009.

Other popular implementations include anacron and fcron. However, anacron is not an independent cron program; it relies on another cron program to call it in order to perform.


Components of cron

1: Path to your php or cgi script
2: minutes (Value is 00 – 59. exact minute the cron executes)
3: hour (Value is 00 – 23. hour of the day the cron executes. 0 means midnight)
4: month (Value is 01 – 12. month of the year the cron executes)
5: day (Value is 01 – 31. day of the month the cron executes)
6: weekday (Value is 00 – 06. day of the week the cron executes. Sunday=0,Monday=1…)

Examples on how to set up cron:

If you have installed a php script in your public_html/www/html directory called members.php.

If you want to run this file each night at 11.30 PM every day:

30 23 * * * php /home/username/www/members.php
30 – represents the minute of cron work
23 – represents the hour of the day
The * represent every day, month, and weekday.

If you want to set the cron job every sunday at midnight 11.30 PM:

30 23 * * 0 /home/username/www/members.php
0 – represents the Sunday.

If you want the cron job to run at 1:00 and 2:00 A.M:

* 1,2 * * * /home/username/www/members.php
This runs your cron at 1A.M and 2A.M every day, every month and every week.

If you want to run the above task only from Monday to Friday and at 11.30 P.M:

30 23 * * 1-5 /home/username/www/members.php
This runs your cron from Monday to Friday and everyday at 11.30 PM.

Setting up the cron:

* METHOD ONE: Using Cron tab manager

If you have a cron tab manager in your webpanel, it will be easy for you to set the cron tab function. You can click on the icon to open cron manager. There will be some textarea where you can enter the the script file path that you want the cron job to executes.

Your cron manager may be different from the above image, but basically it is easy to set up cron job with your cron manager. I highly recommend this unless you want to get to know about the Second Telnet Method.

* METHOD TWO: Uploading ‘cron.txt’ file and checking using telnet


* Cron Commands:

crontab filename Install filename as your crontab file
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.

* Telnet Steps:

Basically FOUR steps:
1. Create cron.txt
2. Upload
3. Install the txt file as cron file with the command ‘crontab cron.txt’
4. Check your cron file

1. Open your notepad on your computer and write cron job following the guidelines explained above under ‘What are the components of cron?’.

For example – If you write your cron job like this:

30 23 * * * /home/username/www/members.php
After writing the above cron job PRESS return key so that a blank line will be there below the cron job line.

2. Always use absolute path for the command line. Turn off the ‘Word Wrap’ feature with the Notepad.

3. Save the file as ‘cron.txt’. Upload it to your root directory (’/’) where you will see a list of folders like public_html,public_ftp,email etc,. Or ask your web host where to upload the cron files. Upload in ASCII mode.

When you ftp into your site, first you will land on your root directory.

4. Now telnet into your server.

5. You have to use telnet software like putty to login into your server.

At command prompt type -

crontab cron.txt

Press Enter.

You will be back at command prompt.

Then type: (l means lowercase L)

crontab -l

Press Enter.

You will see the list of cron jobs that you have entered in cron.txt.

Cron emails you everytime it runs. How to stop it?

Add this tag at the end of your cron job:

>/dev/null 2>&1

Taking the above example your cron job looks like:

30 23 * * * /home/username/www/members.php>/dev/null 2>&1
TIPS in setting up your cron jobs:

* Ask your host about the procedure if you are not sure of a thing.

* Check your cron once in a while. For example you set your cron to send your ezine at 11.30 PM. Then you subscribe yourself to see it is working or not.

* Upload cron.txt in ASCII mode.

* If you are setting the script path in your schedule, the path SHOULD be from your hosts server root. NOT your domain path.

Ex:/home/user/www/scriptname.php
is correct.

http://www.yourdomain.com/scriptname.php
is incorrect.

* Script names are case sensitive on Unix. So be careful in entering the script name.

* If you are creating cron.txt file and uploading, you should not allow spaces within 1-5 components except between them. (For example 10,30 * * * * is correct not 10 ,30 * * * *)


Tags: , , , , , ,

Copyright © 2010 Sailen’s Blog All rights reserved.