使用 else 动态创建脚本的 Shell_exec

使用 else 动态创建脚本的 Shell_exec

我正在尝试使用 Shell_exec 运行动态创建并存储在 php 变量中的脚本。这是到目前为止的脚本。

{ curl -fsS --retry 3 https://hc-ping.com/same-unique-id-here ; \
echo "name.of.php.file.here STARTED for id # $state_id" ; \
php "/path/to/my.file.php" -i 2 -h prod 2>&1 | tee -a /path/to/log/files/my.file.log || \
curl -fsS --retry 3 https://hc-ping.com/same-unique-id-here/fail ; \
echo "name.of.php.file.here ENDED for id # $state_id with Exit Code $?" ; \
curl -fsS --retry 3 https://hc-ping.com/same-unique-id-here ; } 2>/dev/null >/dev/null &

我的第一个问题是我只想执行最后一行

curl -fsS --retry 3 https://hc-ping.com/same-unique-id-here

如果

php "/path/to/my.file.php" -i 2 -h prod 2>&1 | tee -a /path/to/log/files/my.file.log

是成功的。我如何修改脚本来做到这一点?

我的第二个问题,这是我在回显结束时打印退出代码的方式吗?

echo "name.of.php.file.here ENDED for id # $state_id with Exit Code $?"

答案1

我希望就这个问题有更多的互动。我最终使用一个变量来控制执行的内容。这是代码。

{ unset -v failcode ; failcode="0" ; curl -fsS --retry 3 https://hc-ping.com/same-unique-value ; \
echo "\n$(TZ="America/New_York" date +"%m-%d-%Y %H:%M:%S %Z") my.process.name.php STARTED for id # 2" ; \
php "/path/to/my/php/script.php" -i 2 -h lab || failcode="1" ; \
echo "\n$(TZ="America/New_York" date +"%m-%d-%Y %H:%M:%S %Z") my.process.name.php ENDED for id # 2 with Exit Code $failcode" ; \
[ "$failcode" == "1" ] && curl -fsS --retry 3 https://hc-ping.com/same-unique-value/fail ; \
[ "$failcode" == "1" ] && exit 1 ; \
curl -fsS --retry 3 https://hc-ping.com/same-unique-value ; } \
2>&1 | tee -a /path/to/my/log/file.log 2>/dev/null >/dev/null &

相关内容