Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  shell  >  正文 linux进程检测与自动重启的脚本一例

linux进程检测与自动重启的脚本一例

发布时间:2014-10-16   编辑:www.jquerycn.cn
介绍一个shell脚本,可用于检测linux进程,当发现进程停止时便自动重启。有需要的朋友,参考下吧。

该shell脚本,实现如下的功能:
检测systest.sh 进程是否存在,并且进程数只能为1个,如果该进程数不为1或没有运行,则会在3秒内自动重启该脚本。
在使用时,可以调整sleep 为1 和修改需要检测的进程名和路径,可以实现自己的检测脚本。

注意:
该脚本不能放在cron 中进行运行。
可以放在/etc/rc.local 中启动的时候运行一次即可。

代码如下:
 

复制代码 代码示例:

#!/bin/sh
#edit by www.jquerycn.cn

checkprocess()
{
 if [ "$1" = "" ];
   then
      return 1
 fi
process_num=`ps -ef |grep "$1" |grep -v "grep" |wc -l`
if [ $process_num -eq 1 ];
   then
      return 0
   else
      return 1
fi

}

while [ 1 ] ; do
   checkprocess "checkHangupEvent.pl"
   check_result=$?
  if [ $check_result -eq 1 ];
     then
       killall -9 systest.sh
       nohup /root/script/systest.sh &
  fi
  sleep 3
done

您可能感兴趣的文章:
linux进程检测与自动重启的脚本一例
linux下监视进程挂掉后自动重启的shell脚本
一个自动监控进程的shell脚本
shell脚本监控php-fpm并自动重启服务
php定时执行(windows与linux)
检测linux负载过高时重启php的shell脚本
检测linux网络服务是否开启的shell脚本(图文)
ubuntu下使用chkconfig命令
Linux 系统启动过程
PostgreSQL从菜鸟到专家系列教程(6)Linux和Unix上配置PostgreSQL

[关闭]