Shell实现判断进程是否存在并重新启动脚本

「爱情、让人受尽委屈。」 2022-09-25 15:14 307阅读 0赞

简洁版:

  1. #! /bin/bash
  2. # author caoxin
  3. # time 2012-10-10
  4. # program : 判断进行是否存在,并重新启动
  5. function check(){
  6. count=`ps -ef |grep $1 |grep -v "grep" |wc -l`
  7. #echo $count
  8. if [ 0 == $count ];then
  9. nohup python /runscript/working/$1 &
  10. fi
  11. }
  12. check behaviors.py

详细版:

  1. #!/bin/bash
  2. #
  3. #调用关闭jboss进程脚本
  4. stopMethodServer.sh
  5. #打印出当前的jboss进程:grep jboss查询的jboss进程,grep -v "grep" 去掉grep进程
  6. jmsThread=`ps -ef | grep gdms | grep jboss | grep -v "grep"`
  7. echo $jmsThread
  8. #查询jboss进程个数:wc -l 返回行数
  9. count=`ps -ef | grep gdms | grep jboss | grep -v "grep" | wc -l`
  10. echo $count
  11. sec=7
  12. #开始一个循环,以判断进程是否关闭
  13. for var in 1 2
  14. do
  15. if [ $count -gt 0 ]; then
  16. #若进程还未关闭,则脚本sleep几秒
  17. echo sleep $sec second the $var time, the JMS thread is still alive
  18. sleep $sec
  19. else
  20. #若进程已经关闭,则跳出循环
  21. echo "break"
  22. break
  23. fi
  24. done
  25. #if [ $count -eq 0 ]; then
  26. # echo "nohup startMethodServer.sh &"
  27. # nohup startMethodServer.sh &
  28. #else
  29. # echo "It's better to check the thread!!!"
  30. #fi
  31. #调用启动脚本
  32. nohup startMethodServer.sh &

发表评论

表情:
评论列表 (有 0 条评论,307人围观)

还没有评论,来说两句吧...

相关阅读