日志类shell脚本
Apache日志文件切割
#!/bin/bash year=`date -d '-1 day' +%Y` month=`date -d '-1 day' +%m` day=`date -d '-1 day' +%d` mkdir /var/log/httpd/$year/$month -p mv /var/log/httpd/access_log /var/log/httpd/$year/$month/access_log-$day touch /var/log/httpd/access_log kill -HUP `cat /var/run/httpd/httpd.pid` 然后结合放到cron计划任务每天晚上01点01分作
Nginx日志文件切割
#!/bin/bash #nginx log path logs_path="/usr/local/nginx/logs/" mkdir -P ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/ mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +%Y)/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log kill -USR1 `cat /usr/local/nginx/nginx.pid`
logrotate脚本
#!/bin/sh y=`date "+%Y"` m=`date "+%m"` d=`date "+%d"` cd /mnt/apache-tomcat-2-8080/logs cp catalina.out catalina.out.$y$m$d echo > catalina.out cd /mnt/apache-tomcat-3-80/logs cp catalina.out catalina.out.$y$m$d echo > catalina.out exit
日志里的时间比北京时间要晚8个小时,处理成北京时间
#!/bin/bash touch /var/log/newrsyncd.log sed '' /var/log/rsyncd.log | while read message do time=`echo $message | awk '{print $1" "$2}'` newtime=`date +%Y-%m-%d" "%k:%M:%S -d "$time 8 hour"` newmessage=`echo $message | awk -F[ '{print $2}'` if [ `echo $newtime | awk '{print $2}' | awk -F: '{print $1}'` -le 9 ] then newtime=`echo $newtime | awk '{print $1" 0"$2}'` fi echo $newtime" ["$newmessage >> /var/log/newrsyncd.log done mv -f /var/log/newrsyncd.log /var/log/rsyncd.log