对linux 主机配置定时清理 | ntssl.cn

对linux 主机配置定时清理

对linux 主机配置定时清理

因为经常有一些脚本会执行超时,而脚本本身又没有配置超时的配置,所以就需要有个脚本来定时对超时的程序进行清理。

很简洁的小脚本,请查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash     
#by wangcc
function write_log {
if [ $# != 2 ]
then
echo "Usage: "$0" need: 1. log level; 2. message !!! "$0" exit!!!"
exit 0
fi

now=`date +%Y-%m-%d-%H:%M:%S`
echo "${now}:[$1]--->>>$2<<<"
}

function check_free {
buff=`free -m | grep Mem | awk '{print $6}'`
if [[ $buff -gt 1000 ]]
then
echo 1 > /proc/sys/vm/drop_caches
sleep 2
echo 2 > /proc/sys/vm/drop_caches
write_log "INFO" "wanted clean free .......Cleaning up"
fi

}

function kill_jincheng {
ancient_pid=`ps -ef |grep -E 'ip_request|get_goubanjia_ip|get_check'|grep -v grep | awk '{print $2}'|awk BEGIN{RS=EOF}'{gsub(/\n/,"|");print}' `
pid=`ps -eo pid,tty,user,comm,lstart,etime | grep -E ${ancient_pid} | awk -F'[ :]+' '{if ($13>50) print $2}'`
for i in ${pid} ;
do
if [[ ${i} -gt 0 ]]
then
kill -9 ${pid}

fi
done

}

function main {
check_free
kill_jincheng
}

main

配置定时任务

*/5 * * * * sh /root/scripts/shell/monitor/monitor.sh

大功告成