I'm running a very-very slow PHP script on a shared server using cronjob. (the script performes some HTTP requests which suspend its execution on the server from time to time... it completes execution in ~15-20 seconds.)
The problem is that the cronjob terminates from time to time, help desk of shared server told me it shouldn't run more than 5 seconds consecutively.
So I've thought (before getting to serious optimization task) to execute the script and return immediately while the script keeps on running. Till now I thought about two ways:
- Cronjob won't wait for the PHP script to finish its job.
- Cronjob will execute a dummy PHP script which will call the main PHP script and return immediately without waiting for the second script to return.
I've read about both options on the web and especially in this website, and didn't find something that really works: multi_curl doesn't do anything if the first script finish immediately...
Thanks in advance.
Edit with possible solution I've just came up with:
header("Content-Length: 0");
header("Connection: close");
flush();
$mainUrl = 'mySecondScript.php';
$active= null;
$mch = curl_multi_init();
$ch1 = curl_init($mainUrl);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_multi_add_handle($mch, $ch1);
$resultCurl = curl_multi_exec($mch, $active);
do {
$resultCurl = curl_multi_exec($mch, $active);
} while ($resultCurl == CURLM_CALL_MULTI_PERFORM);
It may resemble code you may familiar with here or anywhere else from the web - but it wouldn't work if I use it as other people wrote it for me - only like this!