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

爱被打了一巴掌 2022-03-20 11:20 519阅读 0赞

详细版:

  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 &

详细zabbix版本

  1. #!/bin/bash
  2. :<<\EOF
  3. About configuration file scan
  4. Script author is neo_will
  5. EOF
  6. #Print out the current Zabbix process: the zabbix process of the grep JBoss query, and the grep-v "grep" removes the grep process
  7. Port_Zabbix=` ps -ef|grep zabbix_agentd |grep -v "grep"`
  8. IP_Own=`ip a | grep tap0 | grep inet | awk '{print $2}' | awk -F'/' '{print $1}'`
  9. port=(`ps -ef |grep 'zabbix' | awk '{print $3}'| sed -n '3p' |awk -F':' '{if($NF~/^[0-9]*$/) print $NF}' |sort |uniq`)
  10. echo $Port_Zabbix
  11. sleep 1
  12. #Query the number of zabbix processes: wc-l returns rows
  13. Count_zabbix=`ps -ef | grep zabbix_agentd | grep -v "grep" | wc -l`
  14. echo $Count_zabbix
  15. sleep 1
  16. #Defining variables
  17. Zabbix_AgentdFile="/usr/local/zabbix/sbin/zabbix_agentd"
  18. #Determine whether a file exists
  19. if [ ! -f "$Zabbix_AgentdFile" ]; then
  20. echo "$IP_Own File exists" >> /opt/zabbix_scanning.txt
  21. else
  22. echo "$IP_Own file does not exist" >> /opt/zabbix_scanning.txt && bash zabbix_agent3-2-0guoke666666.sh
  23. fi
  24. sleep 60
  25. sec=7
  26. #Start a loop to determine if the process is closed
  27. for var in 1 2
  28. do
  29. if [ $count -gt 0 ]; then
  30. #If the process is not closed, the script sleep for a few seconds
  31. echo sleep $sec second the $var time, the $IP_Own zabbix thread is still alive >> /opt/zabbix_scanning.txt
  32. sleep $sec
  33. else
  34. #If the process is closed, jump out of the loop
  35. echo "If the $IP_Own process is closed, jump out of the loop and break " >> /opt/zabbix_scanning.txt
  36. service zabbix_agentd restart && break
  37. fi
  38. done

发表评论

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

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

相关阅读