Shell脚本监控程序运行情况(结束进程)

Shell脚本监控程序运行情况(结束进程)

上一篇博客写到了重启程序,那么结束程序该怎么写脚本呢?
还是用前面的例子:我有个程序是qt生成的可执行文件,名字为Manipulator,路径是/home/mk90/Documents/qt_exercise/build-Manipulator-Desktop-Debug,它的监控脚本如下:
kill.sh

#! /bin/bash

while true 
do
	monitoring=` ps -ef|grep Manipulator |grep -v grep| wc -l`
	if [ $monitoring -eq 0 ] 
	then
		echo "Manipulator is not running "
	else
		echo "Manipulator is running, and kill it"
		ps -ef| grep Manipulator| grep -v grep| awk '{print $2}'| xargs kill -9 #xargs 将前面的结果作为参数给 kill-9
	fi
	sleep 3
done

其中指令的含义在上一篇
https://mp.csdn.net/mdeditor/94588412#
里面详细解释过了,不再重复,要重点说明的是这一行:

ps -ef| grep Manipulator| grep -v grep| awk '{print $2}'| xargs kill -9 #xargs 将前面的结果作为参数给 kill-9

从前面一直到 awk ‘{print $2}’ 的含义是:以详细格式查看所有进程,从中选出具有关键字 Manipulator 的进程,但是排除掉用于查找的grep自身进程,对于满足上面条件的结果,打印其第二列(进程的pid号)。

xargs kill -9 : 这几个代码的意思是把前面指令运行的结果当做参数传递给后面的指令,在这里,前面的指令运行的结果是找到了进程的pid号,到了这里就相当于kill -9 [pidof target], 也就是根据进程的pid 号来结束进程。xargs 命令是个很有意思的命令,有兴趣的话可以参考下面的文章:
https://www.cnblogs.com/wangqiguo/p/6464234.html

这样,整个脚本就可以没3秒钟查看一下有没有目标进程,如果有的话就结束它。

还有另外一个方法:

#! /bin/bash

while true 
do
	monitoring=` ps -ef|grep Manipulator |grep -v grep| wc -l`
	target=` ps -ef|grep Manipulator |grep -v grep|awk '{print $2}'`
	if [ $monitoring -eq 0 ] 
	then
		echo "Manipulator is not running "
	else
		echo "Manipulator is not running, and kill it"
		kill -9 $target
	fi
	sleep 3
done

这种方法与前面的不同在于,这里是先查找到进程的pid号,赋值给一个变量target,在下面将target的值传递给 kill -9 指令。当然从本质上来说这两种是一样的。

到了这里,我们可以结合前面重启程序的方法,做一个无聊的小游戏~~~
有两个程序,test.py 和 test2.py ,有两个脚本 yes.sh 和 no.sh,yes.sh 脚本目的是让 test.py 程序运行,所以它一旦检测到 test.py 程序没有运行,就重启它,并且如果发现 test2.py 程序运行,就结束它; no.sh 正好相反,会结束 test.py ,重启 test2.py。
如下:
test.py

#!/usr/bin python
import time
while True:
	time.sleep(1)
	print 'Love!!!'

test2.py

#!/usr/bin python
import time
while True:
	time.sleep(1)
	print 'Not Love!!!'

简单到令人发指。。。。。。不要在意这些,主要看逻辑

yes.sh

#! /bin/bash

while true 
do
	monitor1=`ps -ef | grep test.py | grep -v grep | wc -l ` 
	monitor2=`ps -ef | grep test2.py | grep -v grep | wc -l ` 
	if [ $monitor2 -eq 0 ] 
	then
		echo "test2.py is not running"
	else
		echo "test2.py program is running, and kill it"
		ps -ef|grep test2.py|grep -v grep|awk '{print $2}'|xargs kill -9 
	fi

	if [ $monitor1 -eq 0 ] 
	then
		echo "test.py is not running, restart test.py"
		python /home/mk90/Documents/restart_pro/test.py
	else
		echo "test.py program is running"
	fi
	sleep 5
done

另一个脚本
no.sh

#! /bin/bash

while true 
do
	monitor1=`ps -ef | grep test.py | grep -v grep | wc -l ` 
	monitor2=`ps -ef | grep test2.py | grep -v grep | wc -l ` 
	if [ $monitor1 -eq 0 ] 
	then
		echo "test.py is not running"
	else
		echo "test.py is running, and kill it"
		ps -ef|grep test.py|grep -v grep|awk '{print $2}'|xargs kill -9 
	fi

	if [ $monitor2 -eq 0 ] 
	then
		echo "test2.py is not running, restart test2.py"
		python /home/mk90/Documents/restart_pro/test2.py
	else
		echo "test2.py is running"
	fi
	sleep 5
done

里面涉及到的东西都解释过了,monitor1 和monitor2 分别查找 test.py 和 test2.py 进程运行情况, yes.sh 脚本里面先看 test2.py 是否运行,是的话先结束它,然后看test.py 是否运行,没有的话启动它,no.sh 正好相反。

注意:
要把用来结束程序的那几行指令放在前面,因为一旦启动程序的话,由于要运行的程序是一直循环运行,就无法执行下面的指令。解决的办法可以用:

		gnome-terminal -x bash -c "python /home/mk90/Documents/restart_pro/test.py; exec bash"

但是这样也有一个问题,就是每次启动一次程序,就会新开一个终端,用不了一会儿屏幕就满了。

最后的结果如下:
Shell脚本监控程序运行情况(结束进程)_第1张图片
就这样,两个程序相爱相杀,就像爱情一样,纠缠不清,直到时间的尽头~~~~
当然,除了我打断它们~~~

你可能感兴趣的:(shell)