0

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:

  1. Cronjob won't wait for the PHP script to finish its job.
  2. 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!

1 Answer 1

1

If the user PHP runs under on your hosting has execute permissions, you could use shell_exec and the & to make it run a background process, so something like this in your PHP file: shell_exec('/path/to/php /path/to/file >/dev/null 2>&1 &')

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Michoel, I've tried it before and from some reason it caused a ~50 times loop of calling my first script - a phenomenon I cannot explain (all my first script job was calling the second script, and it didn't.) Anyways meantime I think I've found a dirty solution composited from two snapshot I googled before (which both didn't work as is), I'll edit my first message maybe it will help others or one can improve my fix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.