标签:
在linux系统维护过程中,经常需要解决各种各样的需求,最简单的方式还是用linux系统本身的shell,最简单有效
这个网站很好推荐给大家点这里
1.释放内存:
cat monitor_mem.sh
#!/bin/bash
used=`free -m | awk ‘NR==2‘ | awk ‘{print $3}‘`
free=`free -m | awk ‘NR==2‘ | awk ‘{print $4}‘`
echo " date:`date +%Y%m%d%H%M` ===========================" > /var/log/mem.log
date >> /var/log/mem.log
echo "Memory usage | [Use:${used}MB][Free:${free}MB]" >> /var/log/mem.log
if [ $free -le 4000 ] ; then
sync && echo 1 > /proc/sys/vm/drop_caches
sync && echo 2 > /proc/sys/vm/drop_caches
sync && echo 3 > /proc/sys/vm/drop_caches
echo "OK" >> /var/log/mem.log
mail -s "`hostname` mem monitor info" 15001027302@139.com < /var/log/mem.log
else
echo "Not required" >> /var/log/mem.log
fi
exit
2,列数据转为行
cat test.txt
192.168.5.44
192.168.5.45
192.168.5.46
192.168.5.47
192.168.5.51
192.168.5.52
192.168.5.54
192.168.5.55
sed ‘:a;$!N;s/\n/,/;ta‘ test.txt
192.168.5.44,192.168.5.45,192.168.5.46,192.168.5.47,192.168.5.51,192.168.5.52,192.168.5.54,192.168.5.55
3,获取进程运行的时间
cat ./get_process_time.sh
#!/bin/bash
function show_elapsed_time()
{
user_hz=$(getconf CLK_TCK) #mostly it‘s 100 on x86/x86_64
pid=$1
jiffies=$(cat /proc/$pid/stat | cut -d" " -f22)
sys_uptime=$(cat /proc/uptime | cut -d" " -f1)
last_time=$(( ${sys_uptime%.*} - $jiffies/$user_hz ))
echo "the process $pid lasts for $last_time seconds."
}
if [ $# -ge 1 ];then
for pid in $@
do
show_elapsed_time $pid
done
fi
while read pid
do
show_elapsed_time $pid
done
执行示例:
./get_process_time.sh 4260
4,获取进程运行的时间并杀死过期进程,以免占用内存及swap空间
cat ./get_process_time.sh
#!/bin/bash
#set -x
function show_elapsed_time()
{
user_hz=$(getconf CLK_TCK) #mostly it‘s 100 on x86/x86_64
pid=$1
oneday=86400
jiffies=$(cat /proc/$pid/stat | cut -d" " -f22)
sys_uptime=$(cat /proc/uptime | cut -d" " -f1)
last_time=$(( ${sys_uptime%.*} - $jiffies/$user_hz ))
if [ $last_time -gt $oneday ];then
echo "the process $pid lasts for $last_time seconds."
kill -9 $pid
else
echo "no exits gt $oneday process"
fi
}
if [ $# -ge 1 ];then
for pid in $@
do
show_elapsed_time $pid
done
fi
执行示例:
./get_process_time.sh `ps aux | grep java| grep -v grep |awk ‘{print $2}‘|sed ‘:a;$!N;s/\n/ /;ta‘`
标签:
原文地址:http://www.cnblogs.com/shantu/p/4598893.html