Skip to content

Instantly share code, notes, and snippets.

@noname007
Last active August 21, 2019 08:43
Show Gist options
  • 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

背景&目标:

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

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

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

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