帮助中心 >  技术知识库 >  云服务器 >  服务器教程 >  利用cronolog按每小时切割nginx日志的设置方法

利用cronolog按每小时切割nginx日志的设置方法

2016-08-09 08:03:11 4888

1.mkfifo /map/logs/nginx/wireless/access.log

 nohup cat /map/app/nginx-1.2.2/logs/error.log | /map/app/tools/cronolog-1.7.0/sbin/cronolog  /map/logs/nginx/wireless/error/%Y/%m/%d/%H.log &

 nohup cat /map/logs/nginx/wireless/access.log | /map/app/tools/cronolog-1.7.0/sbin/cronolog  /map/logs/nginx/wireless/%Y/%m/%d/%H.log &

注意:在启动nginx前必须先启动cronolog,否则无法启动
2.把cronolog增加到nginx启动脚本中
脚本内容如下:
#!/bin/sh
#
# Comments to support chkconfig
#
# chkconfig: 2345 77 77
# Description: Startup script for nginx webserver
#
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/map/app/nginx/sbin/$NAME
CONFIGFILE=/map/app/nginx/conf/nginx.conf
PIDFILE=/map/app/nginx/logs/$NAME.pid
SERVICENAME=/etc/init.d/nginxd-map
cronolog=/map/app/tools/cronolog-1.7.0/sbin/cronolog
ErrorLog=/map/app/nginx-1.0.6/logs/error.log
AccessLog=/map/logs/nginx/wireless/access.log
##红色部分是新增加的,检测文件是否是pipe类型,启动cronolog,杀掉cronolog,很简单,但是能够实现功能
d_mkfifo(){
[ ! -p $ErrorLog ] || [ ! -p $AccessLog ]  &&  /bin/rm $ErrorLog $AccessLog  && mkfifo $ErrorLog $AccessLog
}
cronolog_start()
{
#ps -ef|grep wireless|grep -v grep |awk '{print $2}'|xargs kill -9
nohup cat /map/app/nginx-1.0.6/logs/error.log | $cronolog  /map/logs/nginx/wireless/error/%Y/%m/%d/%H.log &
nohup cat /map/logs/nginx/wireless/access.log | $cronolog  /map/logs/nginx/wireless/%Y/%m/%d/%H.log &
}
cronolog_kill()
{
ps -ef|grep wireless|grep -v grep |awk '{print $2}'|xargs kill -9
}
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
d_start() {
d_mkfifo
cronolog_start
  $DAEMON -c $CONFIGFILE || echo -n " already running"
}
d_stop() {
#  kill -QUIT `cat $PIDFILE` || echo -n " not running"
cronolog_kill
killall nginx || echo -n " not running"
}
d_reload() {
  kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}

case "$1" in
  start)
        echo -n "Starting $DESC: $NAME"
        d_start
        echo "."
        ;;
  stop)
        echo -n "Stopping $DESC: $NAME"
        d_stop
        echo "."
        ;;
  reload)
        echo -n "Reloading $DESC configuration..."
        d_reload
        echo "reloaded."
        ;;
  status)
        pgrep -x $NAME > /dev/null && echo $NAME (pid `pgrep $NAME`) is running...  || echo "$NAME is stoped."
        ;;
  test|-t)
        $DAEMON -t
        ;;
  restart)
        echo -n "Restarting $DESC: $NAME"
        d_stop
        sleep 2
        d_start
        echo "...done!"
        ;;
  *)
          echo "Usage: $SERVICENAME {start|stop|restart|reload|status|check}"
          exit 3
        ;;
esac
exit 0


提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: