csdn/CSDN博文备份/Linux使用logrotate日志归档-143809549.md

1 line
7.0 KiB
Markdown
Raw Permalink Normal View History

2025-02-06 12:21:40 +08:00
<p>系统时时刻刻都在产生日志,如果不及时清理,很快就会灌满硬盘,但如果要手工清理,又很麻烦。</p> <br><p>这种情况下logrotate 这个程序很好的完成这个任务。</p> <br><p>logrotate 用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”。</p> <br><p>我们可以根据日志文件的大小也可以根据其天数来转储这个过程一般通过一个叫做crond的守护进程来执行logrotate 还可以用于压缩日志文件以及发送日志到指定的E-mail 。</p> <br><p>logrotate 的配置文件是 /etc/logrotate.conf。</p> <br><h3>查看当前版本</h3> <br><p>在系统中使用命令来查看版本:</p> <br><p></p> <br><pre><code>logrotate --version<br></code></pre> <br><p></p> <br><p></p> <br><p class="img-center"><a href="https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/3X/a/1/a15ed3287dac0e461fe313a466de129866a77a98.jpeg" rel="nofollow"><img alt="2025-02-05_21-16-15" height="166" src="https://i-blog.csdnimg.cn/img_convert/37dc3519bc41ab6968b0359e8516ff11.jpeg" width="461" /></a></p> <br><p></p> <br><p></p> <br><p>一些使用的方法,请参考下面的内容:</p> <br><h3>查看使用手册</h3> <br><p></p> <br><pre><code>man logrotate<br></code></pre> <br><p></p> <br><p></p> <br><p class="img-center"><a href="https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/3X/e/3/e3b3f93f0c02c85ad62a38348a9308d25ae27916.jpeg" rel="nofollow"><img alt="2025-02-05_21-17-58" height="500" src="https://i-blog.csdnimg.cn/img_convert/aea855671a381e6e7d918f45f81c525a.jpeg" width="611" /></a></p> <br><p></p> <br><h3>nginx 日志归档</h3> <br><p>在默认情况下,操作系统已经为我们配置了 nginx 的默认日志归档。</p> <br><p>归档的配置文件为: <code>/etc/logrotate.d/nginx</code></p> <br><p>可以直接编辑这个文件。</p> <br><p></p> <br><pre><code>/var/log/nginx/*.log<br>/var/log/nginx/src.isharkfly.com/*.log<br>{<br> daily<br> missingok<br> rotate 14<br> compress<br> delaycompress<br> notifempty<br> create 0640 www-data adm<br> sharedscripts<br> prerotate<br> if [ -d /etc/logrotate.d/httpd-prerotate ]; then \<br> run-parts /etc/logrotate.d/httpd-prerotate; \<br> fi \<br> endscript<br> postrotate<br> invoke-rc.d nginx rotate >/dev/null 2>&1<br> endscript<br>}<br></code></pre> <br><p></p> <br><p></p> <br><p class="img-center"><a href="https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/3X/3/8/387dfd32f723fd6c9d072261744a5f70c3995429.jpeg" rel="nofollow"><img alt="2025-02-05_21-35-59" height="491" src="https://i-blog.csdnimg.cn/img_convert/806877eb69b20e64cddbf4b66b30c5a4.jpeg" width="690" /></a></p> <br><p></p> <br><p>对我们来说,我们虚拟主机的日志和 nginx 的日志在相同的目录下,只是在下面添加了一个文件夹。</p> <br><p>所以,我们就在上面添加文件夹就行。</p> <br><h3>归档测试</h3> <br><p>配置文件完成修改后,可以对配置进行测试:</p> <br><p>运行命令:</p> <br><p></p> <br><pre><code>logrotate /etc/logrotate.d/nginx --debug<br></code></pre> <br><p>服务器上输出的内容为:</p> <br><p></p> <br><pre><code>root@ns564012:/etc/logrotate.d# logrotate /etc/logrotate.d/nginx --debug<br>warning: logrotate in debug mode does nothing except printing debug messages! Consider using verbose mode (-v) instead if this is not what you want.<br><br>reading config file /etc/logrotate.d/nginx<br>Reading state from file: /var/lib/logrotate/status<br>Allocating hash table for state file, size 64 entries<br>Creating new state<br>Creating new state<br>Creating new state<br>Creating new state<br>Creating new state<br>Creating new state<br>Creating new state<br>Creating new state<br>Creating new state<br>Creating new state<br>Creating new state<br>Creating new state<br>Creating new state<br>Creating new state