标准版:
#!/bin/bash
while true
do
r=`ps -ef | grep /home/test/httpserver | grep -v grep | wc -l`
if [ $r -eq 0 ]
then
sudo pkill httpserver
/home/test/httpserver &
fi
sleep 3s
done
扩展版:
#! /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