Shell指令碼監控CPU利用率平滑重新啓動進程

2020-08-11 17:29:46

Process

#! /bin/bash

counter=0

utilization=`top -n 2 | fgrep "Cpu(s)" | tail -n 1 | awk '{printf "%s", $2}'`

echo $utilization

if [ `echo "$utilization > 90" | bc` -eq 1 ]; then
    ((counter++))
fi

echo $counter

sleep 30

if [ $counter -eq 1 ]; then
	utilization=`top -n 2 | fgrep "Cpu(s)" | tail -n 1 | awk '{printf "%s", $2}'`

    echo $utilization

	if [ `echo "$utilization > 90" | bc` -eq 1 ]; then
	    kill -USR2 `cat /opt/php/php71/var/run/php-fpm.pid`
	fi
fi

Conclusion

不同的版本,對應的檔案路徑會有些出入,以上僅供參考,請君靈活處理。

References

Linux shell 得到 CPU 使用率

linux top命令詳解

tail 命令

linux管道pipe詳解

Linux awk 命令

Linux —— Shell程式設計之變數賦值和參照

shell中的if語句

shell中的if語句

Shell變數:Shell變數的定義、賦值和刪除

Linux bc 命令

轉載本文,請註明出處、作者