0

I would like to run a php script in the background as it takes much time and I don't want to wait for it.

This question relates to:

php execute a background process

I tried to make this work on my linux centos machine, but unfortunately it seems not to be working.

This is my code:

$cmd = 'php '.realpath($_SERVER["DOCUMENT_ROOT"]).'/test/sample.php';

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
exec(sprintf("$s > $s 2>&1 & echo $1", $cmd, $outputfile),$pidArr); 

sleep(2);

print_r($outputfile);
print_r($pidfile);

I included the sleep(2) to make sure the sample.php script is finished. The sample.php only contains echo 'Hello world'; I tried both exec options, but none of them worked. The above script doesn't show any output.

I tried to run the same $cmd in linux command line and it showed me the output from sample.php script.

I would like to make this run first, but in addition I would like to send variables to the sample.php script as well.

Your help is very appreciated.

Community
  • 1
  • 1
BastiaanWW
  • 1,259
  • 4
  • 18
  • 34
  • 1
    Token/cron: leaving a token, which can be a line in a file or a database entry or something similar, that is searched for by a long-running cron job and controls its behavior is often a good alternative to running background processes from the browser. An admin has better control over how often the cron job runs, what permissions it has, and what it is doing. – Paul Jul 06 '13 at 10:50
  • Thanks but I want to run it inmediately when a user visits this php script. – BastiaanWW Jul 06 '13 at 10:57

3 Answers3

2

You can use set_time_limit(0) to allow script to run indefinitely and ignore_user_abort() to keep running script in background, even if the browser of tab have been closed. This way if you redirect your browser to somewhere else, script will keep running in background.

  • Not sure if `ignore_user_abort` will work, since running exec will not create a tty. Additionally, you could end up with a large number of long running scripts, depending on what the script does. – Chris Henry Jul 06 '13 at 15:23
1

This approach to running php scripts in the background seems a bit awkward. And depending on if you pass additional parameters based on user input, could also create a scenario where an attacker could inject additional commands.

For use cases like this, you could be using cron, if you don't need a response immediately, or a job manager like Gearman, where you can use a Gearman server to manage the communication between the we request and the background job.

Chris Henry
  • 11,914
  • 3
  • 30
  • 31
0

I made some adjustments

$cmd = 'php '.realpath($_SERVER["DOCUMENT_ROOT"]).'/test/sample.php';
$outputfile = 'ouput.txt';
$pidfile = 'pid.txt';
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
exec(sprintf("$s > $s 2>&1 & echo $1", $cmd, $outputfile),$pidArr); 

sleep(2);

print_r(file_get_contents($outputfile));
print_r(file_get_contents($pidfile));

Not sure if you defined the variables before. I tested it on Windows and it works

tlogbon
  • 1,212
  • 13
  • 12