Get exit status from php script inside shell script

I have a bash script shell that calls several PHP scripts like this.

#!/bin/bash php -f somescript.php php -f anotherscript.php 

I want to compile an error log and / or activity report based on the results of these scenarios.

Is there a way to get php script exit status in a shell script?

I could use integer exit statuses or string messages.

+7
bash php shell exit-code
source share
3 answers

You can easily catch the output using the backtick operator and get the exit code of the last command using $ ?:

 #!/bin/bash output=`php -f somescript.php` exitcode=$? anotheroutput=`php -f anotherscript.php` anotherexitcode=$? 
+9
source share

Emilio's answer was good, but I thought I could extend it to others. You can use a script like this in cron if you want, and ask him to send you an email if an error occurs. YAY: D

 #!/bin/sh EMAIL=" myemail@foo.com " PATH=/sbin:/usr/sbin:/usr/bin:/usr/local/bin:/bin export PATH output=`php-cgi -f /www/Web/myscript.php myUrlParam=1` #echo $output if [ "$output" = "0" ]; then echo "Success :D" fi if [ "$output" = "1" ]; then echo "Failure D:" mailx -s "Script failed" $EMAIL <<!EOF This is an automated message. The script failed. Output was: $output !EOF fi 

Using php-cgi as a command (instead of php ) simplifies passing url parameters to a PHP script, and they can be accessed using regular PHP code, for example:

$id = $_GET["myUrlParam"];

+2
source share

The $output parameter of the exec command can be used to get the output of another PHP program:

callee.php

 <?php echo "my return string\n"; echo "another return value\n"; exit(20); 

caller.php

 <?php exec("php callee.php", $output, $return_var); print_r(array($output, $return_var)); 

Running caller.php displays the following:

 Array ( [0] => Array ( [0] => my return string [1] => another return value ) [1] => 20 ) 

Note that exit status must be a number in the range 0-254. See exit for more information on return status codes.

+1
source share

All Articles