1、nginx不停服務進行切割日志:
[root@weblogic scripts]# cat nginx_log.sh
#!/bin/bash
log_path=/var/log/nginx
YESTERDAY=`date -d "yesterday" +%Y-%m-%d_%S`
nginx_cmd="/usr/sbin/nginx"
/bin/mv ${log_path}/access.log ${log_path}/access.$YESTERDAY.log
/bin/mv ${log_path}/error.log ${log_path}/error.$YESTERDAY.log
# reopen a new log file
${nginx_cmd} -s reopen
然后進行定時任務設定,設置在凌晨12點:
[root@weblogic nginx]# crontab -l 0 0 * * * /bin/bash /root/scripts/nginx_log.sh
測試效果:
[root@weblogic nginx]# ll 總用量 8 -rw-r--r-- 1 nginx root 0 12月 15 02:13 access.2017-12-14_01.log -rw-r--r-- 1 nginx root 0 12月 15 08:04 access.log -rw-r--r-- 1 nginx root 0 12月 15 03:09 error.2017-12-14_01.log -rw-r--r-- 1 nginx root 61 12月 15 08:04 error.log
關於nginx命令中的幾個信號處理,請查看官方文檔:http://nginx.org/en/docs/beginners_guide.html

2、使用kill命令向nginx主進程發送一個信號:
#向nginx主進程發送USR1信號,重新打開日志文件,否則會繼續往mv后的文件寫數據的。原因在於:linux系統中,內核是根據文件描述符來找文件的。如果不這樣操作導致日志切割失敗。
# kill -USR1 `ps axu | grep "nginx: master process" | grep -v grep | awk '{print $2}'`
所以該切割腳本如下:
[root@weblogic scripts]# cat nginx_sin.sh
#!/bin/bash
log_path=/var/log/nginx
YESTERDAY=`date -d "yesterday" +%Y-%m-%d_%S`
nginx_cmd="/usr/sbin/nginx"
/bin/mv ${log_path}/access.log ${log_path}/access.$YESTERDAY.log
/bin/mv ${log_path}/error.log ${log_path}/error.$YESTERDAY.log
# send a signal
/bin/kill -USR1 `ps axu | grep "nginx: master process" | grep -v grep | awk '{print $2}'`
執行結果如下:
[root@weblogic nginx]# ll 總用量 8 -rw-r--r-- 1 nginx root 0 12月 15 08:04 access.2017-12-14_49.log -rw-r--r-- 1 nginx root 0 12月 15 08:28 access.log -rw-r--r-- 1 nginx root 61 12月 15 08:04 error.2017-12-14_49.log -rw-r--r-- 1 nginx root 0 12月 15 08:28 error.log
現在將腳本放進到crontab定時腳本中,凌晨12點執行:
[root@weblogic nginx]# crontab -l 0 0 * * * /bin/bash /root/scripts/nginx_sin.sh
3、使用logrotate進行切割:
1、安裝:
yum -y install logrotate
2、配置logrotate:
[root@weblogic logrotate.d]# pwd /etc/logrotate.d [root@weblogic logrotate.d]# cat nginx /var/log/nginx/*log { daily rotate 10 dateext missingok notifempty # compress delaycompress create 640 nginx adm sharedscripts postrotate [ -f /var/run/nginx.pid ] && /bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || : endscript }
3、配置參數:
/var/log/nginx/為nginx日志的存儲目錄,可以根據實際情況進行修改。
daily:日志文件將按天輪循。
weekly:日志文件將按周輪循。
monthly:日志文件將按月輪循。
missingok:在日志輪循期間,任何錯誤將被忽略,例如“文件無法找到”之類的錯誤。
rotate 7:一次存儲7個日志文件。對於第8個日志文件,時間最久的那個日志文件將被刪除。
dateext:定義日志文件后綴是日期格式,也就是切割后文件是:xxx.log-20160402.gz這樣的格式。如果該參數被注釋掉,切割出來是按數字遞增,即前面說的 xxx.log-1這種格式。
compress:在輪循任務完成后,已輪循的歸檔將使用gzip進行壓縮。
delaycompress:總是與compress選項一起用,delaycompress選項指示logrotate不要將最近的歸檔壓縮,壓縮將在下一次輪循周期進行。這在你或任何軟件仍然需要讀取最新歸檔時很有用。
notifempty:如果是空文件的話,不進行轉儲。
create 640 nginx adm:以指定的權限和用書屬性,創建全新的日志文件,同時logrotate也會重命名原始日志文件。
postrotate/endscript:在所有其它指令完成后,postrotate和endscript里面指定的命令將被執行。在這種情況下,rsyslogd進程將立即再次讀取其配置並繼續運行。注意:這兩個關鍵字必須單獨成行。
4、查看logrotate切割日志的時間:
[root@weblogic logrotate.d]# cat /etc/anacrontab # /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # the maximal random delay added to the base delay of the jobs RANDOM_DELAY=45 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 #period in days delay in minutes job-identifier command 1 5 cron.daily nice run-parts /etc/cron.daily 7 25 cron.weekly nice run-parts /etc/cron.weekly @monthly 45 cron.monthly nice run-parts /etc/cron.monthly
# the jobs will be started during the following hours only START_HOURS_RANGE=3-22
