如何用shell实现守护进程程序呢,用ps获取$PRO_NAME进程数量,然后对进程进行重启、关闭等操作,一例linux shell守护进程脚本,感的朋友参考下。
linux shell实现守护进程脚本
做了一个udhcpd与udhcpc的守护,目前只会用shell模仿编写,还有什么方法可以做守护呢?
shell脚本:
复制代码 代码示例:
#! /bin/sh
#进程名字可修改
PRO_NAME=udhcpc
WLAN=ra0
while true ; do
# 用ps获取$PRO_NAME进程数量
NUM=`ps aux | grep ${PRO_NAME} | grep -v grep |wc -l`
# echo $NUM
# 少于1,重启进程
if [ "${NUM}" -lt "1" ];then
echo "${PRO_NAME} was killed"
${PRO_NAME} -i ${WLAN}
# 大于1,杀掉所有进程,重启
elif [ "${NUM}" -gt "1" ];then
echo "more than 1 ${PRO_NAME},killall ${PRO_NAME}"
killall -9 $PRO_NAME
${PRO_NAME} -i ${WLAN}
fi
# kill僵尸进程
NUM_STAT=`ps aux | grep ${PRO_NAME} | grep T | grep -v grep | wc -l`
if [ "${NUM_STAT}" -gt "0" ];then
killall -9 ${PRO_NAME}
${PRO_NAME} -i ${WLAN}
fi
done
exit 0