Linux timed release cache shell scripts for automated operations

Source: Internet
Author: User

Users of the Linux server should know that when Linux is in use, the available memory will slowly become smaller over time, and it may eventually reach 80%-90%, which requires us to intervene manually to release the Soul village.
This article will use the Linux cron Timer task combined with Shell scripting to automate the memory release.
Basic process
1. Write the shell script to detect the memory usage of the current server and force the memory to be freed if it reaches our preset value.
2. Add the written Shel script to the Cron scheduled task list and set the run cycle.
Releasing the cached shell script
If in the terminal we usually use the following command to free memory

Sync
Echo3>>/proc/sys/vm/drop_caches
The sync command is used to force the changed content to be written to disk immediately, updating the block information
Echo 3 >>/proc/sys/vm/drop_caches command is a command that forces the release of the cache

The shell script freememory.sh is given below

#!/bin/sh
#内存总量
total=$ (free-m|grep Mem |awk ' {print $} ')
#已使用内存
used=$ (free-m|grep Mem |awk ' {print $} ')
#内存使用率
rate=$ (($used *100/$total))
Time=date
echo "############$ (date) ###############"
#$1 is Max Mem
if[$rate-gt$1]
Then
echo "rate= $rate freememory start!"
Sync
Echo 3 >>/proc/sys/vm/drop_caches
echo "Freememory success!"
Else
echo "rate= $rate Memory is normal"
Fi

1. Execute command crontab-e open Edit Timer profile
2. Add the following command to the last side of the file:

0 */root/freememory.sh 70>>/root/freememorylog.txt
Above we set a daily 23 point execution Command Our script, the parameter setting 70, is when the memory utilization is detected more than 70 when the release cache command back to execute, the following >> is the redirect command, The execution results of the script are written into the file freeMemoryLog.txt for easy viewing.
Cron does not need to be restarted, and the file will automatically take effect when it is saved.

Linux timed release cache shell scripts for automated operations

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.