Skip to content

Instantly share code, notes, and snippets.

@noname007
Last active August 21, 2019 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noname007/9fd71eba97619211e76a9586741d5281 to your computer and use it in GitHub Desktop.
Save noname007/9fd71eba97619211e76a9586741d5281 to your computer and use it in GitHub Desktop.
#!/bin/bash
#https://blog.csdn.net/mydriverc2/article/details/79011438
set -x
cmd="$@"
r=$(ps aux|grep "${cmd}" | grep -v "$0"|grep -v 'grep')
if [ "x${r}" == 'x' ]; then
echo "exec:[${cmd} &]"
$($@) &
sleep 5
echo "$?"
r=$(ps aux|grep "${cmd}" | grep -v "$0"|grep -v 'grep')
if [ "x${r}" == 'x' ]; then
wait $!
ret_code=$?
echo "${ret_code}, $!"
exit ${ret_code}
fi
fi
@noname007
Copy link
Author

noname007 commented Aug 21, 2019

success

$ ./up.sh ./yii hello/index
+ cmd='./yii hello/index'
++ grep './yii hello/index'
++ ps aux
++ grep -v ./up.sh
++ grep -v grep
+ r=
+ '[' x == x ']'
+ echo 'exec:[./yii hello/index &]'
exec:[./yii hello/index &]
+ sleep 5
++ ./yii hello/index
+ echo 0
0
++ ps aux
++ grep './yii hello/index'
++ grep -v ./up.sh
++ grep -v grep
+ r='soul11201         58554   0.0  0.2  4359904  18452 s009  S+    4:18PM   0:00.12 php ./yii hello/index'
+ '[' 'soul11201         58554   0.0  0.2  4359904  18452 s009  S+    4:18PM   0:00.12 php ./yii hello/index' == x ']'

$ echo $?
0

failed

$ ./up.sh ./yii hello/index
+ cmd='./yii hello/index'
++ grep './yii hello/index'
++ ps aux
++ grep -v ./up.sh
++ grep -v grep
+ r=
+ '[' x == x ']'
+ echo 'exec:[./yii hello/index &]'
exec:[./yii hello/index &]
+ sleep 3
++ ./yii hello/index
PHP Notice 'yii\base\ErrorException' with message 'Undefined index: kafka-config'

in ./commands/HelloController.php:156

Stack trace:
#0 ./commands/HelloController.php(156): yii\base\ErrorHandler->handleError(8, 'Undefined index...', './commands/HelloC...', 156, Array)
#1 [internal function]: app\commands\HelloController->actionIndex()
#2 ./vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#3 ./vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#4 ./vendor/yiisoft/yii2/console/Controller.php(148): yii\base\Controller->runAction('index', Array)
#5 ./vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('index', Array)
#6 ./vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('hello/i...', Array)
#7 ./vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('hello/i...', Array)
#8 ./vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request))
#9 ./yii(20): yii\base\Application->run()
#10 {main}
+ echo 0
0
++ ps aux
++ grep './yii hello/index'
++ grep -v ./up.sh
++ grep -v grep
+ r=
+ '[' x == x ']'
+ wait 58514
+ ret_code=1
+ echo '1, 58514'
1, 58514
+ exit 1

$ echo $?
1

@noname007
Copy link
Author

noname007 commented Aug 21, 2019

背景&目标:

shell 中的 & 会掩盖掉 原本脚本运行的状态码。

如果 dcron 拉起来失败,这段代码可以传递出来原本脚本的进程的退出错误码。

如果后台运行进程没有拉起来,也能感知到失败了,继续复用 dcron 的邮件、短信

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment