Crontab+Shell do Nginx log cut script instance code

  • 2020-05-13 04:32:58
  • OfStack

In normal days, we need to output the error log of Nginx to the file, but the file is extremely large after a long time. I left the log on the server for a week before, but today I have seen 32 G log files, which is terrible. So I wrote a log cut script.

The principle of

The principle of log cutting scripts is simple

First, move the original log file to the specified folder Send USR1 to Nginx and have Nginx reload the configuration file [if no signal is sent, nginx will still write the log to the file you moved

#!/bin/bash

LOGS_PATH=/home/wwwlogs 
DATE=$(date +%Y-%m-%d) 
TIME=$(date +%H)

if [ ! -d ${LOGS_PATH}/${DATE}]; then 
 mkdir ${LOGS_PATH}/${DATE}
fi

mv ${LOGS_PATH}/error.log ${LOGS_PATH}/${DATE}/error_${TIME}.log 
#  to  nginx  Send a signal to reload the configuration file 
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)

The code above is fairly simple, which is to generate folders by date, move the files to a new folder, and then use Kill to send a signal to reload the configuration. The USR1 signal 1 is generally a user-defined signal that developers can define how to handle, and in Nginx the configuration is reloaded.

Get the script running at the specified time

When it comes to timing tasks, Linux has no bigger name than Crontab. The following is a brief introduction to the use of Crontab.

Start the crontab service

1 is generally /sbin/service crond start

See if the service is running


$ ps -ax | grep cron

crontab command

crontab -u : sets a user's cron service, which is required by 1 general root user to execute this command

crontab -l crontab-r: delete the cron service crontab-e: edit the cron service for a user

crontab grammar

小时 星期 命令
0-59 0-23 1-31 1-12 0-6 command

The meaning of a few special symbols: - * represents the number taken in the range - / represents each - - represents a number from a number to a number - separating discrete Numbers

The sample

小时 星期 命令
5 * * * * ls 指定每小时的第5分钟执行1次ls命令
30 5 * * * ls 指定每天的 5:30 执行ls命令
30 7 8 * * ls 指定每月8号的7:30分执行ls命令
30 5 8 6 * ls 指定每年的6月8日5:30执行ls命令
30 6 * * 0 ls 指定每星期日的6:30执行ls命令[注:0表示星期天,1表示星期1
30 3 10,20 * * ls 每月10号及20号的3:30执行ls命令[注:“,”用来连接多个不连续的时段]
25 8-11 * * * ls 每天8-11点的第25分钟执行ls命令[注:“-”用来连接连续的时段]
*/15 * * * * ls 每15分钟执行1次ls命令 [即每个小时的第0 15 30 45 60分钟执行ls命令 ]
30 6 */10 * * ls 每个月中,每隔10天6:30执行1次ls命令[即每月的1、11、21、31日的6:30执行1次ls 命令。 ]
50 7 * * * root run-parts /etc/cron.daily [ 注:run-parts参数表示,执行后面目录中的所有可执行文件。 ]

Execute our script with crontab

After a brief understanding of crontab, we can start to use crontab to execute our script regularly.

Open the editor with crontab-e

Enter the following in the editor:


59 23 * * * /home/nginx-cut.sh

This means that the script is executed at 23:59 am each day. - save the file, then use crontab-l to see the timing task we added

The important thing to note here is that nginx needs the root permission to signal it, so crontab-e needs to be executed under root


Related articles: