When using CentOS, the process of individual programs will continue to grow until it goes down. But the program itself has no better version of the use (such as Ngrok free of charge, this is not the problem), so want to write a script timed to detect the situation of the program, decide whether to restart, restart the restart, Let him start without booting.
Note: It is best not to include NGROKD in the script name, because the script executes itself as a process, and the process size of the original NGROKD cannot be shortened togrep "Ngrokd"instead of the full path: grep "/USR/LOCAL/NGROK/BIN/NGROKD", and Pkill will end the script itself, but in order to more general the following by the full path to write, do not use shorthand, the name of the foot can be arbitrarily written.
script function: When the NGROKD process consumes more memory thanat 10000, the process restarts, and if there is no process, the process is started.
NGROKD Program in:/USR/LOCAL/NGROK/BIN/NGROKD, can be modified according to their own circumstances
script Name: restart-ngrokd.sh
The script is as follows, put this script in the/usr/local/sbin directory:
#!/bin/bash
#/usr/local/ngrok/bin/ngrokd -domain=xiejl.com -httpAddr=:80 &> /dev/null &
param=" &> /dev/null &"
memsize=`ps -aux | grep "/usr/local/ngrok/bin/ngrokd" | grep -v grep | awk ‘{printf $6 }‘`
echo $memsize
if [[ $memsize -gt 10000 ]]; then
echo "restart begin"
ngrok_restart=`ps -aux | grep "/usr/local/ngrok/bin/ngrokd" | grep -v grep | awk ‘{for(i=11;i<=NF;i++)printf $i " "}‘`
echo $ngrok_restart
pkill -f
/usr/local/ngrok/bin/ngrokd
eval "$ngrok_restart $param"
echo "restart over"
elif [ -z $memsize ]; then
echo "start begin"
eval "/usr/local/ngrok/bin/ngrokd -domain=xiejl.com -httpAddr=:80 $param"
echo "start over"
fi
Script explanation: param variable is to let Ngrok this program background execution of parameters
Memsize variable is the memory footprint of Ngrok
if [[$memsize-GT 10000]]; Then here can't write single brackets [xxx], jointhe $memsize is empty,write [xxx] will go wrong, so use [[xxx]]Eval is the concatenation of the life and string parameters, executed as a command. PKILL-F process full name. End matching full name, add-F parameter
awk ' {for (i=11;i<=nf;i++) printf $i ""} "is the entire 11th column in the previous result is truncated.
Join a timed task and restart the program periodically: check the time and time zone:
date -R
If the result is not +0800, it is not the East eight, not China time. Can be modified to China time:
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Check the time again to see if it has changed back.
To join a timed task, 4 o'clock in the morning restart:
crontab -e
Add to:
0 4 * * * /usr/local/sbin/restart-ngrokd.sh
The program restarts automatically every day 4 o'clock in the morning, without worrying that the program will cause the system to crash.
CentOS7 timing detection process takes up memory size and performs restart process operation (xjl456852 original)