CentOS7 timing detection process takes up memory size and performs restart process operation (xjl456852 original)

Source: Internet
Author: User
Tags pkill

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:
  
 
  1. #!/bin/bash
  2. #/usr/local/ngrok/bin/ngrokd -domain=xiejl.com -httpAddr=:80 &> /dev/null &
  3. param=" &> /dev/null &"
  4. memsize=`ps -aux | grep "/usr/local/ngrok/bin/ngrokd" | grep -v grep | awk ‘{printf $6 }‘`
  5. echo $memsize
  6. if [[ $memsize -gt 10000 ]]; then
  7. echo "restart begin"
  8. ngrok_restart=`ps -aux | grep "/usr/local/ngrok/bin/ngrokd" | grep -v grep | awk ‘{for(i=11;i<=NF;i++)printf $i " "}‘`
  9. echo $ngrok_restart
  10. pkill -f /usr/local/ngrok/bin/ngrokd
  11. eval "$ngrok_restart $param"
  12. echo "restart over"
  13. elif [ -z $memsize ]; then
  14. echo "start begin"
  15. eval "/usr/local/ngrok/bin/ngrokd -domain=xiejl.com -httpAddr=:80 $param"
  16. echo "start over"
  17. 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:
 
   
  
  1. date -R
If the result is not +0800, it is not the East eight, not China time. Can be modified to China time:
 
   
  
  1. 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:
 
   
  
  1. crontab -e
Add to:
 
   
  
  1. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.