方法一: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"`