Skip to content

How to set Cron Jobs in PHP ?

Cron job is scheduling some tasks with in regular interval on server.To setup cron job we must know about the cron tab commonds and shell access to remote server. generally every hosting providers provide some programs like cPanel. Here we run a cron job that runs every minutes automatically without corn tab unix / linux demon or without any external programs.

Generally Cron job will run at least four hours gap on cPanel or Plesk ,so it is not possible to setup cron job every minute unless you have crontab demon on server , SSH account on server. Here we write a simple php script that calls itself at every minute.

Remember this cron job runs with curl php extension.

//This function runs with curl extension that calls or open remote file.
function call_remote_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
}
 
function do_somework(){
//This function to run every minutes
@mail('[email protected]','cron job','cron executed');
}
set_time_limit(0);
// some web hosts will have only 30 sec max execution time so we keep it unlimited time
// this function only runs if safe mode is off
$url="http://www.arjun.net.in/cronjob.php"; //it is the script that runs exactly every minutes
call_remote_file($url);
do_somework();//in mean time run some work
sleep(60); // wait the script for 60 seconds or every minute
call_remote_file($url); // call this script itself after a minute
// This process continues every minute

To stop this cron job simple remove the file from server.

With this script we don’t need

      SSH /SFTP account
      No need of learning cron tab commons.
      No script php exec permissions.
      No need to turn off php safe mode.
0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments