In order to more easily know the memory usage on the server, and according to the memory use size to restart the service, I only the whole fault alarm, if you want to automatically restart the service, we will have to modify their own.
Script content:
It's written, it's not very good-looking, and it's not very good with awk, it's not going to be output in awk, so use a long string of characters to improve
The code is as follows |
Copy Code |
[ZZ ~/my_scripts]$ Cat Awk_monitor { ofs= "T" if ($ >= 5.0) if ($ >=3.0) Print $, $ $, $ $, $5,$6,$7,$8,$9, $, $11, $ #这里我觉得可以改进 Fi } [ZZ ~/my_scripts]$ Cat Monitor_processes #!/bin/bash Top-b-N 1 |sed-n ' 8,$ ' p > Top.log Print "Please notice CPU and memory Usage:n" >> Top_re.log Echo >> Top_re.log Print "PID USER PR NI virt RES SHR S%cpu%mem time+ COMMAND" >> t Op_re.log # I think I can improve here too Gawk-f Awk_monitor < Top.log >> Top_re.log Mail-s "System Monitor" zl@shili001 < Top_re.log |
Later on the internet to find a good code to share with you.
VI check_mem.sh
The code is as follows |
Copy Code |
#!/bin/bash ip= ' Ifconfig | grep ' inet ' | Grep-v ' 127.0.0.1 ' | Awk-f ' {print $} ' | Awk-f ': ' {print $} ' | Head-n 1 ' Max_mem=95 Max_swap=20 memory= ' Free-m|grep Mem|awk ' {print $} ' men= ' Free | awk '/mem/{print int ($3/$2*100)} ' Mem= ' Free-m | awk '/-/{print $} ' swap= ' Free | awk '/swap/{print int ($3/$2*100)} ' If [$Men-gt $MAX _mem-o $SWAP-gt $MAX _swap];then echo "Men warning,memory: $memory, Mem: $Mem" | Mutt-s "$IP Men Warning" rocdk@163.com Fi |
Save it and add it to the crontab.
code is as follows |
copy code |
chmod +x/ root/soft_shell/check_mem.sh */3 * * */bin/sh/root/soft_shell/check_mem.sh |