Original: http://blog.csdn.net/timchen525/article/details/76474017
Problem Scenario:
Develop the shell script to determine the size of the system's remaining memory, if less than 100MB, the message to the system administrator, and the script into the system scheduled tasks, that is, every 3 minutes to perform a check.
In two steps:
First step: scripting
The script memory_mon.sh is as follows:
[Plain]View PlainCopy
- #!/bin/bash
- Freemem= ' Free-m|awk ' nr==3 {print $NF} ' #获取系统当前的内存值, assign to variable Freemem
- chars= "Current memory is$freemem." #定义字符串CHARS变量, as output and for message body use
- If [$FreeMem-lt 100]
- Then
- echo $CHARS |tee/tmp/messages.txt #屏幕输出提示 and write to file
- Mail-s "' Date +%f-%t ' $CHARS" [email protected]</tmp/messages.txt
- Fi
Analysis:
free-m is displayed in megabytes, nr==3 represents the third row, $NF represents the last column. The specific meanings of the individual parameters of free refer to my blog post: theLinux free command analyzes memory usage .
The tee is output to both the Echo $CHARS on the standard output device and outputs the results to a file.
mail-s usage refer to my blog: Linux outbound Mail feature.
Step Two: timed task crontab
[Plain]View PlainCopy
- */3 * * * */bin/sh/root/memory_mon.sh&>/dev/null
(turn) Develop monitoring Linux memory Shell scripts