用ab每隔30分鐘併發一次休息10分鐘

linux腳本監控程序運行狀況(重啓程序)
主要有兩種狀況:
一種是一個可執行文件:如shell腳本文件另外一種是使用python打開的多個程序。
第一種:它的進程名字由路徑名字和程序名字組成,好比:
我有個可執行文件,名字爲testab.sh,路徑是/test
輸入命令:ps -ef | grep testab.sh | grep -v grephtml

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

 

能夠看到,當testab.sh執行的時候,grep -v grep  會顯示該進程;若是testab.sh沒有執行,則什麼都不會顯示。python

 

 第一種狀況:監控的程序是一個可執行程序:以下linux

1、腳本以下:正則表達式

#!/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 " 程序測試結束!"

2、使用shell腳本監控進程,若是進程中止,從新啓動它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]#

 腳本解釋:express

#! /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 grepbash

ps -ef | grep python | grep -v grep

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

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

都獲得結果app

 

 

若是使用上面的那條指令,在有多個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.
相關文章
相關標籤/搜索