用ab每隔30分钟并发一次休息10分钟

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 738100 -c 410 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 0 ] 
    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[0]=0
    Array[1]=1

    Array[0]=`pgrep test.py | sed -n 1p | awk '{print $1}'`
    Array[1]=`pgrep test2.py | 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"
        python test.py
    fi

    if [ ${Array[1]} ]
    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[0]=`pgrep test.py | sed -n 1p | awk '{print $1}'`
    Array[1]=`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.

posted on 2019-11-08 10:14  古风尘  阅读(928)  评论(0编辑  收藏  举报

导航