Linux 定时切割日志任务

方法一:vi  /etc/crontab

方法二:crontab -e

系统定时任务配置文件:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

1 0 * * * /home/logs/Document/rotate_nginx_log.sh

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

让任务生效

设置完成后,重启cron即可使设置的计划任务定时执行了,重启命令如下:
有些linux系统是service cron restart
crond服务的重启命令是
service cron stop
service cron start吧
重启命令:service crond restart
查看任务状态命令
[root@ovp-l5520-216 binlog]# service crond status
crond (pid  1332) 正在运行...


切割日志脚本:

#!/bin/bash
#
# This script used for rotating NGINX log files.
# Typically, we should setup a Linux CRON job to execute this script periodically.
#
# For example, we setup the following CRON job to rotate NGINX log file at mid night.
# 1 0 * * * ${SCHOOL_ANALYTICS_HOME}/release_current/scripts/rotate_nginx_log.sh
#
# Last modified: 2014.12.12 by XnOU

# the NGINX log file being rotated.
NGINX_LOG=/data/nginx/logs/wuxianji_access.log

# the base directory used to store history log files.
BASE_DIR=/data01/wuxianji/binlog/history_logs

#******************************************************************************

# does the NGINX log file exists?
if [ ! -f ${NGINX_LOG} ]; then
    echo "The NGINX log ${NGINX_LOG} does not exists."
    exit 1
fi


# extract the first date from the log file.
DATE_STR=`head -8 ${NGINX_LOG} | tail -1 | awk '{print $4}' | awk -F '[' '{print $2}' | awk -F ':' '{print $1}' | sed 's/\//-/g'`

# format "15/Mar/2014" to "2014"
LOG_YEAR=`date -d${DATE_STR} +%Y`

# format "15/Mar/2014" to "20140315"
LOG_DATE=`date -d${DATE_STR} +%Y%m%d`


# the directory used to store history log files.
HISTORY_LOG_DIR=${BASE_DIR}/${LOG_YEAR}

# create directory if not exists.
mkdir -p ${HISTORY_LOG_DIR}


# move the NGINX log file to destination directory.
DEST_FILE=${HISTORY_LOG_DIR}/${LOG_DATE}.log
mv ${NGINX_LOG} ${DEST_FILE}
#echo $DEST_FILE

# grep the master NGINX process and reproduce log files.
NGINX_PID=`ps aux | grep "sbin/nginx" | grep "master process" | awk '{print $2}'`
kill -USR1 ${NGINX_PID}
sleep 1

echo `date "+%Y-%m-%d %H:%M:%S"`


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值