My first cron

Few days ago when I was working on unseenapps, I came across a functionality which must be executed repeatedly after a certain interval of time. I thought of it and later find out the simplest way.
It was the cpanels in-built functionality called cron jobs.

So what a cron job is?
Well, a cron job is a cpanel module that allows you to run a certain command at times set by the jobs.
For example, you could set a cron job to delete temporary files every week so that your disk space is not being used by those files.

How to setup a cron jobs?
Basically you will want to run a PHP script file in specific intervals. Suppose you want to execute a php file called cronjob.php every one hour. This is what you do:

The CRON Command is in the Following Format

[ Minute – Hour – Day – Month – Weekday ] – Command

The COMMAND, can be broken down in

[PATH OF PHP] [ARGUMENTS] [PATH OF PHP SCRIPT]

So the COMPLETE CRON command can be written as

[ Minute – Hour – Day – Month – Weekday ] [PATH OF PHP] [ARGUMENTS] [PATH OF PHP SCRIPT]

The timing is specified using * symbols

* * * * * => Execute every minute
0 * * * * => Execute every Hour
0 0 * * * => Execute every mid-night
0 0 0 * * => Execute every Month
0 0 0 0 * => Execute every Weekday

Since this is an UNIX command, you will have to mention the PATH TO PHP.
PATH TO PHP: /usr/local/bin/php

ARGUMENTS:
Following are the various command line arguments, but –q(Quiet mode) is widely used.
-a Run interactively
-b

| Bind Path for external FASTCGI Server mode
-C Do not chdir to the script’s directory
-c | Look for php.ini file in this directory
-n No php.ini file will be used
-d foo[=bar] Define INI entry foo with value ‘bar’
-e Generate extended information for debugger/profiler
-f Parse . Implies `-q’
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-q Quiet-mode. Suppress HTTP Header output.
-s Display colour syntax highlighted source.
-v Version number
-w Display source with stripped comments and whitespace.
-z Load Zend extension .

PATH TO PHP SCRIPT

The path to php script must be complete absolute path.
If you have an account at GoDaddy, and your username is “ravidhavlesha”, your path will be
“/home/ravidhavlesha/public_html/”

So lets say that ravidhavlesha want to execute my script “cronjobs.php” every hour. And it is stored in “public_html/cron/ cronjobs.php”.

So the complete command would be,

0 * * * * /usr/local/bin/php -q /home/ravidhavlesha/ public_html/cron/ cronjobs.php

If ravidhavlesha wants to execute every minute, he would use

* * * * * /usr/local/bin/php -q /home/ravidhavlesha/ public_html/cron/ cronjobs.php

For every month

0 0 0 * * /usr/local/bin/php -q /home/ravidhavlesha/ public_html/cron/ cronjobs.php

And so on…
This is how I accomplished cron jobs functionality in my site.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s