Sunday, March 4, 2012

Cron Job example with PHP and Linux

today i am going to show you a practical example about cron job in linux environment. i extremely believe that you know what is a cron job and what is the purpose of using a crone job. if you dont know just google and get a proper understanding before proceeding with this article.

here i have assumed that you have successfully installed LAMP server in your PC and php projects are running without having any issue. today i am going to show you how to run Cron job in linux that executes the executes the php script in every 1 minute. the php script will insert a record to the given database table at each execution.

1. first set up the database and the table as follows.
/* create the database */
create database cron_job_example;

/* use the database */
use cron_job_example;

/* create the table */
create table cron_job_data(
id int,
name varchar(100),
date_time varchar(100));



2. set up the Php Script in the www directory of your LAMP server as follows.

in my case, it is located at /var/www and create a directory called sample_site

then copy the following Php file to the sample_site directory.

sample.php

<?php

//database connection details

$db_server="localhost";
$db_username="root";
$db_password="abc123@#";
$db_name="cron_job_example";

$con = mysql_connect($db_server,$db_username,$db_password);

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db($db_name, $con);

$date = date("Y-m-d");

mysql_query("INSERT INTO cron_job_data (id, name,date_time)
VALUES (1, 'chathuranga','".$date."')");

?>


3. then use Linux terminal to create the cron job file to achieve the target.

you can use any of preferred text editor (vi, gedit etc...)  to create cron file. use following syntax. i have used gedit text editor for this example.

gedit  name_for_file.cron  (it is importnat that you must use .cron extension with the file name)

in my case,

gedit chathuranga.cron


then add the following entry in the newly opened file.

*/1 * * * *  wget http://localhost/cron_job/cron_job_file.php


general syntax of the above entry
<time_specified>  <command>

<time_specified> - */1 * * * *
<command> - wget http://localhost/cron_job/cron_job_file.php

 more description as follows ......

* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59) 


then save the file and use following command in the Linux terminal to execute the cron job you created. (it is good if you can give the full permission(777) for your cron job file before doing following operation)

crontab chathuranga.cron


now your cron job will execute from hereafter.

ps: just google to find more on cronetab commands ;)



hope this will helpful for you!

cheers!!!
Chathuranga Tennakoon
chathuranga.t@gmail.com
chathurangat.blogspot.com




No comments:

Post a Comment