bash 从shell脚本内的php脚本中检索退出状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18647841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:28:47  来源:igfitidea点击:

Retrieve exit status from php script inside of shell script

phpbashshellexit-code

提问by Buttle Butkus

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

我有一个 bash shell 脚本,可以像这样调用一些 PHP 脚本。

#!/bin/bash

php -f somescript.php

php -f anotherscript.php

I want to compose an error log and/or an activity report based on the results of those scripts.

我想根据这些脚本的结果编写错误日志和/或活动报告。

Is there any way I can get the exit status of the php script in the shell script?

有什么办法可以在shell脚本中获取php脚本的退出状态?

I could either use integer exit statuses or string messages.

我可以使用整数退出状态或字符串消息。

回答by Emilio P.

You can easily catch the output using the backtickoperator, and get the exit code of the last commandby using $?:

您可以轻松地赶上使用输出反引号操作符,并获得的退出代码最后一个命令使用$?

#!/bin/bash
output=`php -f somescript.php`
exitcode=$?

anotheroutput=`php -f anotherscript.php`
anotherexitcode=$?

回答by Bastion

Emilio's answer was good but I thought I could extend that a bit for others. You can use a script like this in cron if you like, and have it email you if there is an error.. YAY :D

Emilio 的回答很好,但我想我可以为其他人扩展一点。如果您愿意,您可以在 cron 中使用这样的脚本,如果出现错误,请通过电子邮件发送给您.. YAY :D

#!/bin/sh

EMAIL="[email protected]"
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-cgias the command (instead of php) makes it easier to pass url parameters to the php script, and these can be accessed using the usual php code eg:

使用php-cgias 命令(而不是php)可以更轻松地将 url 参数传递给 php 脚本,并且可以使用通常的 php 代码访问这些参数,例如:

$id = $_GET["myUrlParam"];

$id = $_GET["myUrlParam"];

回答by leepowers

The $outputparameter of the execcommand can be used to get the output of another PHP program:

该命令的$output参数exec可用于获取另一个 PHP 程序的输出:

callee.php

被调用者.php

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

caller.php

调用者.php

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

Running caller.php will output the following:

运行 caller.php 将输出以下内容:

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

    [1] => 20
)

Note the exitstatus must be number in the range of 0-254. See exitfor more info on return status codes.

请注意,exit状态必须是 0-254 范围内的数字。有关exit返回状态代码的更多信息,请参阅。