1月 29

CentOS 下收集CPU温度

  服务器自动重启,怀疑与温度过高有关。直接检测cpu温度,等重启后,查看是否温度问题。
  操作系统CentOS 6.2 ,内核必须是6.22以上的。使用senosor。

# yum install -y lm_sensors
配置
# sensors-detect

载入模块
# modprobe coretemp

查看温度
# sensors
成功看到CPU温度。

用脚本放在crontab中循环跑,等重启了查查看是否温度过高。
#!/bin/sh
log=’/root/temperature.log’
get_data=`date +’%Y-%m-%d %k:%M’`
echo $get_data >> $log
echo `sensors` >> $log

6月 06

linux 两个文件取交集命令

  有两个文件a和b,a中包含b中所有数据。需要找出a文件中包含b文件内容的数据。具体操作如下:
a.txt文件
http://www.simonzhang.net/msg?phone=12320421&msg=hfeaf
http://www.simonzhang.net/msg?phone=13219543&msg=efweff3
http://www.simonzhang.net/msg?phone=12765745&msg=f3fds
http://www.simonzhang.net/msg?phone=12432321&msg=3r2r322
http://www.simonzhang.net/msg?phone=14142142&msg=rgewo3
http://www.simonzhang.net/msg?phone=14212412&msg=iv9e
http://www.simonzhang.net/msg?phone=12321243&msg=e

b.txt文件
12320421
12432321
12321243

  查找a对b的交集命令:grep -f b.txt a.txt
结果如下:
http://www.baidu.com/msg?phone=12320421&msg=hfeaf
http://www.baidu.com/msg?phone=12432321&msg=3r2r322
http://www.baidu.com/msg?phone=12321243&msg=e

举一反三,如果要找差集命令为:grep -v -f b.txt a.txt

5月 15

tomcat 日志切割

日志自动切割以前使用cronolog进行,apache为http://www.simonzhang.net/?p=362,tomcat为http://www.simonzhang.net/?p=359。
但是tomcat7 以后启动叫脚本改了,也就不麻烦了,直接自己写个shell解决,此脚本会丢失少量正在切割时产生的日志。

#!/bin/sh
log_dir=("/program/allweb/tomcat_a/logs" "/program/allweb/tomcat/logs")
for (( i=0 ; i<${#log_dir[@]} ; i++ ))
do
    if [ -f ${log_dir[i]}\/catalina.out ]; then
        cd ${log_dir[i]}
        date_dir=`date +%Y-%m-%d`
        /bin/cp catalina.out  catalina_${date_dir}.out
        /bin/echo '' > catalina.out
        /usr/bin/find -ctime +15 -exec rm -rf {} \;
    fi
done
3月 29

shell 判断字符串长度

        需要循环输出一组序列,序列定义为双位数。所以开始的序列要判断一下字符长度。如果变量为h,计算字符串长度为“${#h}”。这个代码代码如下:

#!/bin/sh
for (( h=0 ; h<20 ;h++ ));do
   if (( ${#h} == 1 ));then
      echo '0'$h
   else
      echo $h
   fi
done

输出结果:
# sh 123.sh
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19