Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP scheduler without cron

Tags:

php

scheduler

I want to run my php script for every 5 minutes. Here is my PHP code.

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);
}
set_time_limit(0);

$root='http://mywebsiteurl'; //remote location of the invoking and the working script

$url=$root."invoker.php";
$workurl=$root."script.php";

call_remote_file($workurl);//call working script
sleep(60*5);// wait for 300 seconds.
call_remote_file($url); //call again this script 

I run this code once. It works perfectly, even after i close the entire browser window.

The problem is the stops working if i turn of my system's internet connect.

How to solve this problem. Please help me out.

like image 981
smk3108 Avatar asked Apr 08 '26 15:04

smk3108


1 Answers

While I wouldn't really recommend doing this for something critical (you're going to have stability issues), this could work:

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);
}
set_time_limit(0);


$root='http://mywebsiteurl'; //remote location of the invoking and the working script

$url=$root."invoker.php";
$workurl=$root."script.php";

while(true)
{
    call_remote_file($workurl);//call working script
    sleep(60*5);// wait for 300 seconds.
}

Another way would be to call it from the command line using exec():

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);
}
set_time_limit(0);


$root='http://mywebsiteurl'; //remote location of the invoking and the working script

$url=$root."invoker.php";
$workurl=$root."script.php";

call_remote_file($workurl);//call working script
sleep(60*5);// wait for 300 seconds.
exec('php ' . $_SERVER['SCRIPT_FILENAME']);

You should really use cron though if at all possible.

like image 106
Andrew Ensley Avatar answered Apr 11 '26 03:04

Andrew Ensley