Using Shell to monitor memory usage scripts in Linux

Source: Internet
Author: User
Tags memory usage

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

Related Article

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.