{"id":2949,"date":"2016-11-26T14:54:37","date_gmt":"2016-11-26T06:54:37","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2949"},"modified":"2016-11-26T14:54:37","modified_gmt":"2016-11-26T06:54:37","slug":"%e5%9c%a8linux%e4%b8%8b%e4%bd%bf%e7%94%a8logrotate%e7%ae%a1%e7%90%86%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2949.html","title":{"rendered":"\u5728Linux\u4e0b\u4f7f\u7528logrotate\u7ba1\u7406\u65e5\u5fd7"},"content":{"rendered":"<p>=Start=<\/p>\n<h4>\u7f18\u7531\uff1a<\/h4>\n<p>logrotate\u662f\u65e5\u5fd7\u5faa\u73af\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u5206\u5272\u65e5\u5fd7\u6587\u4ef6\uff0c\u5220\u9664\u65e7\u7684\u65e5\u5fd7\u6587\u4ef6\uff0c\u521b\u5efa\u65b0\u7684\u65e5\u5fd7\u6587\u4ef6\uff0c\u5faa\u73af\u7ba1\u7406\u65e5\u5fd7\u4ece\u800c\u8282\u7701\u78c1\u76d8\u7a7a\u95f4\u3002<\/p>\n<h4>\u6b63\u6587\uff1a<\/h4>\n<p>\u4e3b\u6d41Linux\u53d1\u884c\u7248\u4e0a\u90fd\u9ed8\u8ba4\u5b89\u88c5\u6709logrotate\u5305\uff0c\u5982\u679c\u51fa\u4e8e\u67d0\u79cd\u539f\u56e0\uff0clogrotate\u6ca1\u6709\u88c5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528apt-get\u6216yum\u547d\u4ee4\u6765\u5b89\u88c5\u3002<\/p>\n<p>\u5728Debian\u6216Ubuntu\u4e0a\uff1a<\/p>\n<pre class=\"lang:default decode:true\">apt-get install logrotate cron<\/pre>\n<p>\u5728Fedora\uff0cCentOS\u6216RHEL\u4e0a\uff1a<\/p>\n<pre class=\"lang:default decode:true\">yum install logrotate crontabs<\/pre>\n<p>logrotate\u7684\u914d\u7f6e\u6587\u4ef6\u662f\/etc\/logrotate.conf\uff0c\u901a\u5e38\u4e0d\u9700\u8981\u5bf9\u5b83\u8fdb\u884c\u4fee\u6539\u3002\u65e5\u5fd7\u6587\u4ef6\u7684\u8f6e\u5faa\u8bbe\u7f6e\u5728\u72ec\u7acb\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u5b83\uff08\u4eec\uff09\u653e\u5728\/etc\/logrotate.d\/\u76ee\u5f55\u4e0b\u3002<\/p>\n<h5>##\u6837\u4f8b\u4e00<\/h5>\n<p>\u5728\u7b2c\u4e00\u4e2a\u6837\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a10MB\u7684\u65e5\u5fd7\u6587\u4ef6\/var\/log\/log-file\u3002\u6211\u4eec\u5c06\u5c55\u793a\u600e\u6837\u4f7f\u7528logrotate\u6765\u7ba1\u7406\u8be5\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<p>\u6211\u4eec\u4ece\u521b\u5efa\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\u5f00\u59cb\uff0c\u7136\u540e\u5728\u5176\u4e2d\u586b\u516510MB\u5927\u5c0f\u7684\u968f\u673a\u6570\u636e\u3002<\/p>\n<p># touch \/var\/log\/log-file<br \/>\n# head -c 10M &lt; \/dev\/urandom &gt; \/var\/log\/log-file<\/p>\n<p>\u73b0\u5728\u65e5\u5fd7\u6587\u4ef6\u5df2\u7ecf\u51c6\u5907\u597d\uff0c\u6211\u4eec\u518d\u914d\u7f6elogrotate\u6765\u8f6e\u5faa\u8be5\u65e5\u5fd7\u6587\u4ef6\u3002\u8ba9\u6211\u4eec\u4e3a\u8be5\u6587\u4ef6\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<pre class=\"lang:default decode:true\"># vim \/etc\/logrotate.d\/log-file\n\/var\/log\/log-file {\n\tmonthly\n\trotate 5\n\tcompress\n\tdelaycompress\n\tmissingok\n\tnotifempty\n\tcreate 644 root root\n\tpostrotate\n\t\t\/usr\/bin\/killall -HUP rsyslogd\n\tendscript\n}<\/pre>\n<p>\u8fd9\u91cc\uff1a<\/p>\n<ul>\n<li>monthly\uff1a\u65e5\u5fd7\u6587\u4ef6\u5c06\u6309\u6708\u8f6e\u5faa\u3002\u5176\u5b83\u53ef\u7528\u503c\u4e3a\u2018daily\u2019\uff0c\u2018weekly\u2019\u6216\u8005\u2018yearly\u2019\u3002<\/li>\n<li>rotate 5\uff1a\u6700\u591a\u5b58\u50a85\u4e2a\u5f52\u6863\u65e5\u5fd7\u3002\u5bf9\u4e8e\u7b2c\u516d\u4e2a\u5f52\u6863\uff0c\u65f6\u95f4\u6700\u4e45\u7684\u5f52\u6863\u5c06\u88ab\u5220\u9664\u3002<\/li>\n<li>compress\uff1a\u5728\u8f6e\u5faa\u4efb\u52a1\u5b8c\u6210\u540e\uff0c\u5df2\u8f6e\u5faa\u7684\u5f52\u6863\u5c06\u4f7f\u7528gzip\u8fdb\u884c\u538b\u7f29\u3002<\/li>\n<li>delaycompress\uff1a\u603b\u662f\u4e0ecompress\u9009\u9879\u4e00\u8d77\u7528\uff0cdelaycompress\u9009\u9879\u6307\u793alogrotate\u4e0d\u8981\u5c06\u6700\u8fd1\u7684\u5f52\u6863\u538b\u7f29\uff0c\u538b\u7f29\u5c06\u5728\u4e0b\u4e00\u6b21\u8f6e\u5faa\u5468\u671f\u8fdb\u884c\u3002\u8fd9\u5728\u4f60\u6216\u4efb\u4f55\u8f6f\u4ef6\u4ecd\u7136\u9700\u8981\u8bfb\u53d6\u6700\u65b0\u5f52\u6863\u65f6\u5f88\u6709\u7528\u3002<\/li>\n<li>missingok\uff1a\u5728\u65e5\u5fd7\u8f6e\u5faa\u671f\u95f4\uff0c\u4efb\u4f55\u9519\u8bef\u5c06\u88ab\u5ffd\u7565\uff0c\u4f8b\u5982\u201c\u6587\u4ef6\u65e0\u6cd5\u627e\u5230\u201d\u4e4b\u7c7b\u7684\u9519\u8bef\u3002<\/li>\n<li>notifempty\uff1a\u5982\u679c\u65e5\u5fd7\u6587\u4ef6\u4e3a\u7a7a\uff0c\u8f6e\u5faa\u4e0d\u4f1a\u8fdb\u884c\u3002<\/li>\n<li>create 644 root root\uff1a\u4ee5\u6307\u5b9a\u7684\u6743\u9650\u521b\u5efa\u5168\u65b0\u7684\u65e5\u5fd7\u6587\u4ef6\uff0c\u540c\u65f6logrotate\u4e5f\u4f1a\u91cd\u547d\u540d\u539f\u59cb\u65e5\u5fd7\u6587\u4ef6\u3002<\/li>\n<li>postrotate\/endscript\uff1a\u5728\u6240\u6709\u5176\u5b83\u6307\u4ee4\u5b8c\u6210\u540e\uff0cpostrotate\u548cendscript\u91cc\u9762\u6307\u5b9a\u7684\u547d\u4ee4\u5c06\u88ab\u6267\u884c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0crsyslogd \u8fdb\u7a0b\u5c06\u7acb\u5373\u518d\u6b21\u8bfb\u53d6\u5176\u914d\u7f6e\u5e76\u7ee7\u7eed\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<p>\u4e0a\u9762\u7684\u6a21\u677f\u662f\u901a\u7528\u7684\uff0c\u800c\u914d\u7f6e\u53c2\u6570\u5219\u6839\u636e\u4f60\u7684\u9700\u6c42\u8fdb\u884c\u8c03\u6574\uff0c\u4e0d\u662f\u6240\u6709\u7684\u53c2\u6570\u90fd\u662f\u5fc5\u8981\u7684\u3002<\/p>\n<h5>##\u6837\u4f8b\u4e8c<\/h5>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ea\u60f3\u8981\u8f6e\u5faa\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u7136\u800c\u65e5\u5fd7\u6587\u4ef6\u5927\u5c0f\u53ef\u4ee5\u589e\u957f\u523050MB\u3002<\/p>\n<pre class=\"lang:default decode:true\"># vim \/etc\/logrotate.d\/log-file\n\/var\/log\/log-file {\n    size=50M\n    rotate 5\n    create 644 root root\n    postrotate\n        \/usr\/bin\/killall -HUP rsyslogd\n    endscript\n}<\/pre>\n<h5>##\u6837\u4f8b\u4e09<\/h5>\n<p>\u6211\u4eec\u60f3\u8981\u8ba9\u65e7\u65e5\u5fd7\u6587\u4ef6\u4ee5\u521b\u5efa\u65e5\u671f\u547d\u540d\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0dateext\u53c2\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<pre class=\"lang:default decode:true\"># vim \/etc\/logrotate.d\/log-file\n\/var\/log\/log-file {\n    monthly\n    rotate 5\n    dateext\n    create 644 root root\n    postrotate\n        \/usr\/bin\/killall -HUP rsyslogd\n    endscript\n}<\/pre>\n<p>\u8fd9\u5c06\u8ba9\u5f52\u6863\u6587\u4ef6\u5728\u5b83\u4eec\u7684\u6587\u4ef6\u540d\u4e2d\u5305\u542b\u65e5\u671f\u4fe1\u606f\u3002<\/p>\n<h5>##\u6392\u969c<\/h5>\n<p>\u8fd9\u91cc\u63d0\u4f9b\u4e86\u4e00\u4e9blogrotate\u63d0\u4f9b\u7684\u6392\u969c\u529f\u80fd\u3002<\/p>\n<h6>#\u624b\u52a8\u8c03\u7528logrotate<\/h6>\n<p>logrotate\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u4ece\u547d\u4ee4\u884c\u624b\u52a8\u8c03\u7528\u3002\u8981\u8c03\u7528\u4e3a\/etc\/lograte.d\/\u4e0b\u914d\u7f6e\u7684\u6240\u6709\u65e5\u5fd7\u8c03\u7528logrotate\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># logrotate \/etc\/logrotate.conf<\/pre>\n<p>\u8981\u4e3a\u67d0\u4e2a\u7279\u5b9a\u7684\u914d\u7f6e\u8c03\u7528logrotate\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># logrotate \/etc\/logrotate.d\/log-file<\/pre>\n<h6>#\u6f14\u7ec3<\/h6>\n<p>\u6392\u969c\u8fc7\u7a0b\u4e2d\u7684\u6700\u4f73\u9009\u62e9\u662f\u4f7f\u7528\u00a0<code>-d<\/code>\u00a0\u9009\u9879\u4ee5debug\u6a21\u5f0f\u8fd0\u884clogrotate\u3002\u4e0d\u7528\u5b9e\u9645\u8f6e\u5faa\u4efb\u4f55\u65e5\u5fd7\u6587\u4ef6\uff0c\u53ef\u4ee5\u6a21\u62df\u6f14\u7ec3\u65e5\u5fd7\u8f6e\u5faa\u5e76\u663e\u793a\u5176\u8f93\u51fa\u3002<\/p>\n<pre class=\"lang:default decode:true\"># logrotate -d \/etc\/logrotate.d\/log-file<\/pre>\n<h6>#\u5f3a\u5236\u8f6e\u5faa<\/h6>\n<p>\u5373\u4f7f\u8f6e\u5faa\u6761\u4ef6\u6ca1\u6709\u6ee1\u8db3\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u00a0<code>-f<\/code>\u00a0\u9009\u9879\u6765\u5f3a\u5236logrotate\u8f6e\u5faa\u65e5\u5fd7\u6587\u4ef6\uff0c\u00a0<code>-v<\/code>\u00a0\u53c2\u6570\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u8f93\u51fa\u3002<\/p>\n<pre class=\"lang:default decode:true\"># logrotate -vf \/etc\/logrotate.d\/log-file<\/pre>\n<h6>#logrotate\u7684\u72b6\u6001\u8bb0\u5f55<\/h6>\n<p>logrotate\u6267\u884c\u7684\u72b6\u6001\u4fe1\u606f\u9ed8\u8ba4\u5b58\u653e\u4e8e\/var\/lib\/logrotate.status\u6587\u4ef6\u4e2d\u3002\u5982\u679c\u51fa\u4e8e\u6392\u969c\u76ee\u7684\uff0c\u6211\u4eec\u60f3\u8981logrotate\u8bb0\u5f55\u81ea\u5df1\u6307\u5b9a\u7684\u6587\u4ef6\u4e2d\u53bb\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u4ece\u547d\u4ee4\u884c\u6307\u5b9a\u3002<\/p>\n<pre class=\"lang:default decode:true\"># logrotate -vf \u2013s \/var\/log\/logrotate-status \/etc\/logrotate.d\/log-file<\/pre>\n<h5>##logrotate\u7684\u5b9a\u65f6\u4efb\u52a1<\/h5>\n<p>logrotate\u9700\u8981\u7684cron\u4efb\u52a1\u5e94\u8be5\u5728\u5b89\u88c5\u65f6\u5c31\u81ea\u52a8\u521b\u5efa\u4e86\uff0c\u4e0b\u9762\u662f\u5177\u4f53\u5185\u5bb9\uff1a<\/p>\n<pre class=\"lang:default decode:true\"># cat \/etc\/cron.daily\/logrotate\n#!\/bin\/sh\n\n\/usr\/sbin\/logrotate \/etc\/logrotate.conf\nEXITVALUE=$?\nif [ $EXITVALUE != 0 ]; then\n    \/usr\/bin\/logger -t logrotate \"ALERT exited abnormally with [$EXITVALUE]\"\nfi\nexit 0<\/pre>\n<h5>\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.lvtao.net\/server\/logrotate.html\">Linux\u65e5\u5fd7\u6587\u4ef6\u603b\u7ba1\u2014\u2014logrotate<\/a><\/li>\n<li><a href=\"https:\/\/www.ifshow.com\/centos-using-logrotate-management-log\/\">CentOS\u4f7f\u7528logrotate\u7ba1\u7406\u65e5\u5fd7<\/a><\/li>\n<\/ul>\n<h5>\u66f4\u591a\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li>man logrotate<\/li>\n<li><a href=\"http:\/\/huoding.com\/2013\/04\/21\/246\">\u88ab\u9057\u5fd8\u7684Logrotate<\/a><\/li>\n<li><a href=\"http:\/\/www.thegeekstuff.com\/2010\/07\/logrotate-examples\/\">HowTo: The Ultimate Logrotate Command Tutorial with 10 Examples<\/a><\/li>\n<li><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-manage-log-files-with-logrotate-on-ubuntu-12-10\">How To Manage Log Files With Logrotate On Ubuntu 12.10<\/a><\/li>\n<li><a href=\"https:\/\/linuxconfig.org\/setting-up-logrotate-on-redhat-linux\">Setting Up Logrotate on RedHat Linux<\/a><\/li>\n<li><a href=\"http:\/\/www.live-in.org\/archives\/874.html\">CentOS Linux\u4f7f\u7528logrotate\u5206\u5272\u7ba1\u7406\u65e5\u5fd7<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/20162176\/centos-linux-setting-logrotate-to-maximum-file-size-for-all-logs\">Centos\/Linux setting logrotate to maximum file size for all logs<\/a><\/li>\n<li><a href=\"http:\/\/www.tecmint.com\/manage-linux-system-logs-using-rsyslogd-and-logrotate\/\">How to Manage System Logs (Configure, Rotate and Import Into Database) in RHEL 7 \u2013 Part 5<\/a><\/li>\n<li><a href=\"http:\/\/www.chenqing.org\/2012\/08\/use-logger-logrotate-manage-your-logs.html\">\u3010linux\u3011\u5229\u7528logger\uff0clogrotate\u5904\u7406\u811a\u672c\u65e5\u5fd7<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a logrotate\u662f\u65e5\u5fd7\u5faa\u73af\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u5206\u5272\u65e5\u5fd7\u6587\u4ef6\uff0c\u5220\u9664\u65e7\u7684\u65e5\u5fd7\u6587\u4ef6\uff0c\u521b\u5efa\u65b0\u7684\u65e5\u5fd7 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,12],"tags":[676,71],"class_list":["post-2949","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-tools","tag-logrotate","tag-tools"],"views":4798,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2949"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2949\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}