-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cron + crontab + logrotate + 定时任务 #102
Comments
Difference between Cron and Crontab?
There are various implementations of cron. Commonly there will be per-user crontab files (accessed with the command In your first example you are scheduling a job via a crontab. In your second example you're using the Specifically, When I schedule a job to be repeated : (Quoting from wiki)
or executing a job only once
Your first example would go in a crontab file. You'd add it via You can use at with no cron daemon running. |
被遗忘的Logrotate问题:sharedscripts的作用是什么?大家可能注意到了,我在前面Nginx的例子里声明日志文件的时候用了星号通配符,也就是说这里可能涉及多个日志文件,比如:access.log和error.log。说到这里大家或许就明白了,sharedscripts的作用是在所有的日志文件都轮转完毕后统一执行一次脚本。如果没有配置这条指令,那么每个日志文件轮转完毕后都会执行一次脚本。 问题:rotate和maxage的区别是什么?它们都是用来控制保存多少日志文件的,区别在于rotate是以个数为单位的,而maxage是以天数为单位的。如果我们是以按天来轮转日志,那么二者的差别就不大了。 问题:为什么生成日志的时间是凌晨四五点?前面我们说过,Logrotate是基于CRON运行的,所以这个时间是由CRON控制的,具体可以查询CRON的配置文件「/etc/crontab」,可以手动改成如23:59等时间执行:
如果使用的是新版CentOS,那么配置文件为:/etc/anacrontab。 问题:如何告诉应用程序重新打开日志文件?以Nginx为例,是通过postrotate指令发送USR1信号来通知Nginx重新打开日志文件的。但是其他的应用程序不一定遵循这样的约定,比如说MySQL是通过flush-logs来重新打开日志文件的。更有甚者,有些应用程序就压根没有提供类似的方法,此时如果想重新打开日志文件,就必须重启服务,但为了高可用性,这往往不能接受。还好Logrotate提供了一个名为copytruncate的指令,此方法采用的是先拷贝再清空的方式,整个过程中日志文件的操作句柄没有发生改变,所以不需要通知应用程序重新打开日志文件,但是需要注意的是,在拷贝和清空之间有一个时间差,所以可能会丢失部分日志数据。 |
使用logrotate管理nginx日志文件例如如果我们想对docker日志进行压缩和分割,则可以设置如下:
|
运维中的日志切割操作梳理(Logrotate/python/shell脚本实现)其他重要参数说明
日志切割logrotate和定时任务crontab详解常见配置参数小结:
|
其他每1分钟切割一次注意:日志里要有内容,因为前面定义空的日志文件是不切割的
每2小时切割一次
|
cron 配置文件
如下几个目录内容配合使用
/etc/crontab
- 这个文件负责安排由系统管理员制定的维护系统以及其他任务的 crontab ;/etc/cron.hourly
- 每小时/etc/cron.daily
- 每天/etc/cron.weekly
- 每周/etc/cron.monthly
- 每月如下目录中的内容是通过
crontab -e
创建的(对应当前用户),不要手动创建;一般用于配合自定义的定时任务使用,比如按分钟、按小时进行日志轮转等;/var/spool/cron/crontabs/
- 这个目录下存放的是每个用户包括 root 的 crontab 任务,每个任务以创建者的名字命名;比如 root 建的 crontab 任务对应的文件就是/var/spool/cron/crontabs/root
;logrotate 配置文件
在基于包管理方式安装完后,出在相应位置出现如下配置文件
/etc/cron.daily/logrotate
/etc/logrotate.conf
/etc/logrotate.d
The text was updated successfully, but these errors were encountered: