linux脚本监控程序运行情况(重启程序)
主要有两种情况:
一种是一个可执行文件:如shell脚本文件;另一种是使用python打开的多个程序。
第一种:它的进程名字由路径名字和程序名字组成,比如:
我有个可执行文件,名字为testab.sh,路径是/test
输入命令:ps -ef | grep testab.sh | grep -v grep

ps -ef | grep testab.sh | grep -v grep

可以看到,当testab.sh执行的时候,grep -v grep  会显示该进程;如果testab.sh没有执行,则什么都不会显示。

第一种情况:监控的程序是一个可执行程序:如下

一、脚本如下:

#!/bin/bash

echo "程序开始启动!"

echo "程序每隔15分钟停一次休息10分钟"

while true
do
echo "本轮循环开始执行! 本次并发测试30分钟!" ab -n -c https://mybank.nbcb.com.cn/cc-test echo " 本轮测试结束 休息10分钟 !" sleep 10m done echo " 程序测试结束!"

二、使用shell脚本监控进程,如果进程停止,重新启动它

[root@localhost test]# cat activetest.sh
#!/bin/bash
echo "本程序是监控 testab.sh 程序,查看其进程是否挂掉,如果挂掉,则重新启动!"
while true
do
  testab=`ps -ef | grep testab.sh | grep -v grep | wc -l`
  if [ $testab -eq 0 ]
  then
    echo "testab.sh program is not running ,restart Manipulator"
    ./testab.sh
  else
    echo "testab.sh program is running"
  fi
  sleep 5
done [root@localhost test]# //-*-*-*-*-*-*testab.sh文件 [root@localhost test]# cat testab.sh
#!/bin/bash
echo "程序开始启动!" echo "程序每隔30秒停一次休息10秒" while true
do
  echo "本轮循环开始执行! 本次并发测试30分钟!"   ab -n 30000 -c 10 https://https://10.20.80.132/   echo " 本轮测试结束 休息10秒 !"
 
  sleep 10s done echo " 程序测试结束!" [root@localhost test]#

脚本解释:

#! /bin/bash

while true
do
ab=`ps -ef | grep testab.sh | grep -v grep | wc -l `
if [ $ab -eq ]
then
echo "testab.sh 进程已经不存在了,请重新启动运行!"
./testab.sh #r若想让程序在后台执行: ./testab.sh &
else
echo "testab.sh 进程仍在运行,无需重启!"
fi
sleep 5s
done
ab=`ps -ef | grep testab.sh | grep -v grep | wc -l`

上面这一句的作用是把后面指令运行的结果赋值给ab,注意等号 “=”前后不要留有空格,如果空格则表示判断是否想等。
注意符合 不是单引号 ,
ps -ef 指令中ps的意思是process status ,即进程状态,-ef是ps命令的选项,表示以详细格式显示所有进程内容。
竖线 “|”称为管道符合,是linux系统一个很强大的功能,表示把前一个命令的输出结果传递给后一个命令的处理。
grep(global search  regular expression(RE) and print out the line,全局搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。后面的ab是要搜索的关键字。
grep -v grep: 其中 -v 是grep命令的选项,表示反向选择,这几个字符表示在前面搜索结果的基础上去除掉带有grep关键字的内容
。因为使用" grep + 关键字" 命令搜索时会有一个grep本身的进程,而且带有搜索的关键字,这个就是要排除自身搜索的影响。
wc -l: wc命令统计指定文件中的字节数、字数、行数、并将统计结果显示输出。选项 "-l "表示统计行数。
综合起来这句指令的意思就是:
以详细格式查看所有进程,从中选出具有关键字testab.sh的进程,但是排除掉用于查找的grep自身进程,对与满足上面条件的结果,统计其行数,也就是看有几个带有testab.sh关键字的进程,将统计的结果赋值给变量ab.
if[ $ab -eq 0 ]:if 语句的判断用test或着 “[]",符合 “$"表示取变量的值, -eq 表示等于,-gt大于,-lt小于,-ge大于等与,-le小于等于。
echo : 用于输出显示。
./testab.sh
用于运行tesab.sh程序

第二种情况,它的进程名字即有python关键字,又有程序名字,但是没有路径。比如有一个test.py程序,使用
python test.py
打开程序,然后在打开一个新的终端输入:
ps -ef | grep python | grep -v grep

ps -ef | grep python | grep -v grep

或者
ps -ef | grep test.py |grep -v grep

ps -ef | grep test.py |grep -v grep

都得到结果

如果使用上面的那条指令,在有多个python程序运行时,显示的进程名字都以python开头,
这时候就需要判断是哪一个python进程了

对使用python打开的多个程序的监控
现在我们有test.py和test2.py两个python程序,现在我要看这两个程序是否已经打开,如果没有就打开他们。
python.sh

脚本如下

#!/bin/bash
declare -a Array while(true)
do
echo -e `date`
Array[]=
Array[]= Array[]=`pgrep test.py | sed -n 1p | awk '{print $1}'`
Array[]=`pgrep test2.py | sed -n 2p | awk '{print $1}'` if [ ${Array[]} ]
then
echo -e "test.py is running!"
else
echo -e "test.py is not running and restart it"
python test.py
fi if [ ${Array[]} ]
then
echo -e "test2.py is running!"
else
echo -e "test2.py is not running and restart it"
python test2.py
fi #clear
echo -e "\n"
sleep 1s done

里面有很多内容在前面的例子里讲过了,需要解释的有以下几点:
declare -a Array : 表示声明了一个数组 Array
echo -e date : 用来打印日期和时间,参数 -e表示激活转义字符,详细可以参考
https://www.cnblogs.com/karl-python/p/9261920.html

    Array[]=`pgrep test.py | sed -n 1p | awk '{print $1}'`
Array[]=`pgrep test2.py | sed -n 2p | awk '{print $1}'`

两条指令的意思就是,查看名为python的进程,把查到的第一个进程的pid号赋值给Array[0],把第二个赋值给Array[1];
后面的判断就是只要有进程pid号,说明进程存在,否则进程不存在。

test.py代码如下:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import time for i in range(100):
print("程序开始执行")
time.sleep(1) print("程序执行结束")

test2.py代码如下?

[root@localhost test]# cat test2.py
#!/usr/bin/env python
#-*- coding:utf-8-*-
import time i=0
print("程序开始执行")
while i<10:
i=i+1
print("第一次相加%d",i)
time.sleep(2) print("程序执行结束!")

你也可以使用终端打开:

#!/bin/bash

declare -a Array

while(true)
do
echo -e `date`
Array[0]=0
Array[1]=1 Array[0]=`pgrep python1 | sed -n 1p | awk '{print $1}'`
Array[1]=`pgrep python2 | sed -n 2p | awk '{print $1}'` if [ ${Array[0]} ]
then
echo -e "test.py is running!"
else
echo -e "test.py is not running and restart it"
gnome-terminal -x bash -c "python /test/test.py; exec bash"
fi if [ ${Array[1]} ]
then
echo -e "test2.py is running!"
else
echo -e "test2.py is not running and restart it"
gnome-terminal -x bash -c "python /test/test2.py; exec bash"
fi #clear
echo -e "\n"
sleep 1 done
		gnome-terminal -x bash -c "python /test/test2.py; exec bash"

这句代码的意思就是打开一个新的终端,执行命令 ”python /home/mk90/Documents/restart_pro/test2.py“,执行完毕后该终端保持存在不关闭。
gnome-terminal 是终端的一种,Ubuntu系统的终端就是这种版本, 参数 -x 表示后面出现的都当做命令执行,并且只执行一次;
bash 是防止终端立即关闭,如果输入:

gnome-terminal -x ls

终端执行后会一闪就关闭,甚至看不到执行的效果;
"-c"选项使shell解释器从一个字符串中而不是从一个文件中读取并执行shell命令;
exec bash 使终端运行命令后仍然存在。

注:

如果打算让程序一值运行:

1. 执行命令后加 & 符号,缺点客户端关了,也会停止执行

后台执行:python.sh  &

显示到前台用命令:fg

又让在后台执行命令:Ctrl+z

2. nohup 命令 &  ,  客户端关了,后台还会在执行

后台执行:nohup pyhton.sh  &

(1) nohup 

加在一个命令的最前面,表示不挂断的运行命令

(2) &

加载一个命令的最后面,表示这个命令放在后台执行

nohup python.sh & 这样程序会在后台运行,技术客户端关闭了,程序也会一种运行。

nohup python.sh & 这样程序会在后台运行,技术客户端关闭了,程序也会一种运行。
[Nohup python.Sh& zhèyàng chéngxù huì zài hòutái yùnxíng, jìshù kèhù duān guānbìle, chéngxù yě huì yī zhǒng yùnxíng.]
nohup python.sh & this program will run in the background, technology client closed, the program will be one operating.
 

最新文章

  1. 在DevExpress中使用CameraControl控件进行摄像头图像采集
  2. 多线程异步导出excel
  3. js学习心得之思维逻辑与对象上下文环境(一)
  4. 将List转换为二维数组(result)
  5. phpstorm相关设置
  6. C#中Guid类型值如何判断不是初始值!
  7. vssver2.scc 文件是干啥的?
  8. UIProgressView 圆角
  9. winfrom播放动态图片
  10. javascript数字验证输入
  11. 连接字符串中Min Pool Size的理解是错误,超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
  12. [原创].NET 分布式架构开发实战五 Framework改进篇
  13. 接口post +json +bean
  14. SoapUI:mock service的使用
  15. (30)批处理文件.bat
  16. class的真相
  17. css 蒙层
  18. 数据库savepoint
  19. Python import容易犯的一个错误
  20. jquery ajax 赋值问题, 后面程序判断逻辑用

热门文章

  1. js点击按钮button效果(波效果)
  2. android studio学习---怎么创建一个新的module并且再次运行起来(在当前的project里面)
  3. MySQL容器化详细教程
  4. AMQP与RabbitMQ
  5. 使用Deployment控制器创建Pods并使Service发布到外网可访问
  6. 修改GIT已提交的用户名和邮箱
  7. vue-router路由高亮效果
  8. mysql5.6采集数据插入出现MySQL server has gone away解决办法
  9. hive使用beeline配置远程连接
  10. 16-C#笔记-枚举