shell脚本检查程序是否挂掉然后重新运行

把该sh加入到开机自启动文件/etc/rc.local中,即可实现开机自启动,程序挂掉重新运行。

#!/bin/sh
while true
do
ps -ef | grep "gateway" | grep -v "grep"
if [ "$?" -eq 1 ]     #上一条命令返回值0成功,1失败
then
    /root/gateway     #要运行的应用程序、脚本等
    echo "process has been restarted!"
else
    echo "process already started!"
fi
sleep 10
done

最后不要忘记了修改新增脚本的权限,执行 chmod 777 checkgateway.sh

你可能感兴趣的:(OpenWrt)