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