0

Possible Duplicate:
Make PHP wait for Matlab script to finish executing

Okay, starting from php execute a background process to run a background process works great. The problem is, I need the return of that process also. The obvious solution to me is :

$cmd = "($cmd > $outputfile 2>&1 || echo $? > $returnfile) & echo $! > $pidfile";
exec($cmd);

When I run the generated command on the command line, it backgrounds and the files are filled out as expected. The problem is that when php exec() runs, the command doesn't go to the background (at least, exec doesn't return until the command finishes). I tried variations with nohup and wait $pid, but still no solution.

Any thoughts?

Community
  • 1
  • 1
celadonz
  • 391
  • 1
  • 8
  • its because `Disallowed system call: SYS_pipe` – NullPoiиteя Oct 30 '12 at 14:53
  • How would you get a return value from something that won't return until it exits? – Marc B Oct 30 '12 at 14:53
  • could try adding & to start of command... usually from shell anyway that's how I start something in the background – Brian Oct 30 '12 at 14:54
  • Also :) If you run in background, I'd expect php will never know it's result! Unless you execute a bit of php and update some db table to query. – Brian Oct 30 '12 at 14:55
  • @brian `( ... )` fires off a subshell to execute the commands within the brackets, and that subshell is being backgrounded. – Marc B Oct 30 '12 at 14:56
  • @brian, I can check the pid stored in the last part to see if when the process finishes as per the linked question. – celadonz Oct 31 '12 at 01:13

1 Answers1

0

This is tricky- you could potentially fork the process to do something else, leaving the original process in place.

http://php.net/manual/en/function.pcntl-fork.php

However, if this is a web application, there's no built-in way to retrieve the return code or STDOUT back into the parent process since it's technically async (your request-response cycle will likely end before a result can be produced).

You could store the return code and / or STDOUT to files to check later, though.

abegosum
  • 636
  • 5
  • 11