Dam*_*man 4 logs ubuntu logrotate ruby
我正在接管一个服务器的管理工作,该服务器有一个定制的 3rd 方 Rails 应用程序。应用程序开发人员告诉我 ruby 日志文件越来越大,并指向我以下链接: - /sf/ask/341872401/
日志位于 /root/production/app/log。该文件夹包含几个以 .log 结尾的文件。当前的日志文件大小约为2GB,文件夹中还有格式为“logname.log.2013_01_18.bz2”的归档日志文件。
我尝试搜索 ssh 命令历史记录以查看是否可以看到用于创建存档文件的命令,但这些命令并没有返回那么远。我还运行了“cat /var/lib/logrotate/status”,但看起来 logrotate 没有旋转上述文件夹中的任何日志。
基本上:
此时我确实需要保留所有日志文件而不是丢弃任何日志文件。欢迎任何有关轮换日志相关问题的信息,例如在尝试轮换之前备份日志。
slm*_*slm 12
logrotate
系统使用它来轮换日志,因此您有 2 个选择。您可以将这些应用程序日志的轮换合并到系统轮换中,也可以设置您自己的轮换,然后手动或从 root 用户的 crontab 运行它们(假设 Rails 应用程序以 root 身份运行,因为它的目录是/root/...
)。
要在系统预先存在的日志中设置 logrotation,只需向目录中添加一个新文件即可/etc/logrotate.d
。叫它railsapp.conf
。我会使用那里的其他示例来构建它。还可以参考logrotate
手册页。
如果您想运行您自己的实例,logrotate
您只需为其提供命令行开关即可。
/etc/logrotate.conf
/root/rails_logrotate.conf
运行
# 1st time
$ logrotate -d -f -s $HOME/my_logrotate.state logrotate.conf
# afterwards
$ logrotate -d -s $HOME/my_logrotate.state logrotate.conf
Run Code Online (Sandbox Code Playgroud)
如果一切正常,您可以在没有-d
开关的情况下重新运行这些命令。这仅用于调试目的,实际上不会执行任何任务,只是向您展示它会做什么。
$ logrotate -s $HOME/my_logrotate.state logrotate.conf
Run Code Online (Sandbox Code Playgroud)
您还可以使用-v
开关使其变得冗长,类似于使用-d
开关时看到的输出。
从这个日志文件开始。
$ dd if=/dev/zero of=afile bs=1k count=10k
10240+0 records in
10240+0 records out
10485760 bytes (10 MB) copied, 0.0702393 s, 149 MB/s
$ ll afile
-rw-rw-r-- 1 saml saml 10485760 Aug 6 14:37 afile
$ touch -t 201307010101 afile
$ ll afile
-rw-rw-r-- 1 saml saml 10485760 Jul 1 01:01 afile
Run Code Online (Sandbox Code Playgroud)现在运行 logrotate
$ logrotate -v -f -s $HOME/my_logrotate.state logrotate.conf
reading config file logrotate.conf
reading config info for /home/saml/afile
Handling 1 logs
rotating pattern: /home/saml/afile forced from command line (1 rotations)
empty log files are rotated, old logs are removed
considering log /home/saml/afile
log needs rotating
rotating log /home/saml/afile, log->rotateCount is 1
dateext suffix '-20130806'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /home/saml/afile to /home/saml/afile-20130806
creating new /home/saml/afile mode = 0664 uid = 500 gid = 501
Run Code Online (Sandbox Code Playgroud)检查结果
$ ll afile*
-rw-rw-r-- 1 saml saml 0 Aug 6 14:40 afile
-rw-rw-r-- 1 saml saml 10485760 Jul 1 01:01 afile-20130806
Run Code Online (Sandbox Code Playgroud)为了让这个每周日运行,你可以为 root 用户创建以下 crontab 条目。
$ crontab -e
Run Code Online (Sandbox Code Playgroud)
添加以下几行:
# 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
0 0 * * sun logrotate -v -f -s $HOME/my_logrotate.state $HOME/logrotate.conf
Run Code Online (Sandbox Code Playgroud)
然后保存上面的。
您还可以使用这些类型的快捷方式,而不是指定实际的天数、分钟、秒等。
string meaning
------ -------
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18605 次 |
最近记录: |