Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting script exit status code in php

Tags:

php

exit-code

Is there anyway to get the exit status code for a php script run in the background via exec($cmd, $output, $exitCode)?

For example:

exec('php script.php &', $output, $exitCode); 

If the trailing '&' isn't included then $exitCode works as expected, it's always 0 otherwise.

like image 786
Parris Varney Avatar asked Dec 30 '10 17:12

Parris Varney


2 Answers

For anybody that finds themselves here, my solution was to make a php script that takes a script as an argument. The new script is called to the background and handles exit statuses appropriately.

For example:

$cmd = 'php asynchronous_script.php -p 1';
exec("php script_executor.php -c'$cmd' &");

The second script looks something like

$opts = getOpt('c:');
$cmd = rtrim($opts['c'], '&');
$exitCode = 0;
$output = '';

exec($cmd, $output, $exitCode);

if ($exitCode > 0) {
 ...
}
like image 66
Parris Varney Avatar answered Sep 22 '22 13:09

Parris Varney


I found something that is probably quite equivalent to Pradeep's solution. Someone posted about this on the function's documentation page. http://www.php.net/manual/en/function.exec.php#101506

like image 29
Kyle Avatar answered Sep 20 '22 13:09

Kyle