{"id":3699,"date":"2012-01-29T20:53:44","date_gmt":"2012-01-29T12:53:44","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3699"},"modified":"2012-01-29T20:56:20","modified_gmt":"2012-01-29T12:56:20","slug":"%e5%88%a0%e9%99%a4%e6%97%a5%e5%bf%97%e6%96%87%e4%bb%b6%e7%9a%84%e7%bb%8f%e5%85%b8%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3699","title":{"rendered":"\u5220\u9664\u65e5\u5fd7\u6587\u4ef6\u7684\u7ecf\u5178\u811a\u672c"},"content":{"rendered":"<p>\u672c\u6587\u6240\u793a\u7684\u5220\u9664\/var\/log\u4e0b\u65e5\u5fd7\u6587\u4ef6\u811a\u672c\u6e90\u4e8eabs\u8fd9\u672c\u4e66\uff0c\u867d\u7136\u5b9e\u9645\u529f\u80fd\u53ea\u662f\u7b80\u5355\u7684\u6e05\u7a7a\u8be5\u76ee\u5f55\u4e0b\u7684messages\u6587\u4ef6\uff0c\u4f46\u662f\u8fd9\u4e2a\u811a\u672c\u5177\u6709\u5e7f\u4e49\u6027\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u5176\u4ed6\u811a\u672c\u7684\u6a21\u677f\u3002<\/p>\n<pre class=\"brush:c\">#!\/bin\/bash\r\n\r\nLOG_DIR=\/var\/log\r\nROOT_UID=0\r\nLINES=50\r\nE_XCD=66\r\nE_NOTROOT=67<\/pre>\n<p>\u4e3a\u4e86\u66f4\u597d\u7684\u79fb\u690d\u6027\uff0c\u4e8b\u5148\u5b9a\u4e49\u51e0\u4e2a\u53d8\u91cf\u3002\u6bd4\u5982\u7528LOG_DIR\u6307\u5b9a\u65e5\u5fd7\u6240\u5728\u76ee\u5f55\uff0cE_\u5f00\u5934\u7684\u53d8\u91cf\u4e3a\u4e0d\u540c\u7684\u9519\u8bef\u7801\u3002<\/p>\n<pre class=\"brush:c\">if [ \"$UID\" -ne \"$ROOT_UID\" ]\r\nthen \r\n\techo \"Must be root to run this script.\"\r\n\texit $E_NOTROOT\r\nfi\r\n\r\nif [ -n \"$1\" ]\r\nthen \r\n\tlines=$1\r\nelse\r\n\tlines=$LINES\r\nfi<\/pre>\n<p>\u7531\u4e8e\u5220\u9664\/var\/log\u4e0b\u7684\u6587\u4ef6\u9700\u8981root\u8eab\u4efd\uff0c\u56e0\u6b64\u4e00\u5f00\u59cb\u5148\u5224\u65ad\u8fd0\u884c\u8be5\u811a\u672c\u7684\u7528\u6237\u662f\u5426\u4e3aroot\u3002\u63a5\u7740\u5224\u65ad\u53c2\u65701\u662f\u5426\u4e3a\u7a7a\uff0clines\u6307\u5b9a\u9ed8\u8ba4\u65e5\u5fd7\u4fdd\u7559\u7684\u884c\u6570\u3002<\/p>\n<pre class=\"brush:c\">cd $LOG_DIR\r\n\r\nif [ `pwd` != \"$LOG_DIR\" ]\r\nthen\r\n\techo \"Can't change to $LOG_DIR.\"\r\n\texit $E_XCD\r\nfi<\/pre>\n<p>\u5224\u65ad\u662f\u5426\u6210\u529f\u8fdb\u5165\u4e86LOG_DIR\u6240\u4ee3\u8868\u7684\u76ee\u5f55\u3002\u4e0a\u8ff0\u4ee3\u7801\u7684\u66f4\u6709\u6548\u7387\u7684\u8868\u793a\u65b9\u6cd5\u4e3a\uff1a<\/p>\n<pre class=\"brush:c\"># cd \/var\/log || {\r\n#   echo \"Cannot change to necessary directory.\" &gt;&amp;2\r\n#   exit $E_XCD;\r\n# }<\/pre>\n<p>\u6700\u540e\u624d\u662f\u5b9e\u8d28\u6027\u7684\u65e5\u5fd7\u5220\u9664\u529f\u80fd\uff1a<\/p>\n<pre class=\"brush:c\">tail -$lines messages &gt; mesg.temp\r\nmv mesg.temp messages\r\n\r\nexit 0<\/pre>\n<p>\u6700\u540e\u53ea\u4fdd\u7559messages\u6587\u4ef6\u4e2dlines\u884c\u65e5\u5fd7\u3002<\/p>\n<p>\u8be5\u811a\u672c\u7684\u529f\u80fd\u867d\u7136\u7b80\u5355\uff0c\u4f46\u662f\u5374\u62e5\u6709\u7528\u6237\u8eab\u4efd\u68c0\u67e5\u3001\u53c2\u6570\u68c0\u67e5\u7b49\u529f\u80fd\uff0c\u5bf9\u5176\u4ed6\u811a\u672c\u800c\u8a00\u662f\u4e2a\u5f88\u597d\u7684\u6a21\u677f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6240\u793a\u7684\u5220\u9664\/var\/log\u4e0b\u65e5\u5fd7\u6587\u4ef6\u811a\u672c\u6e90\u4e8eabs\u8fd9\u672c\u4e66\uff0c\u867d\u7136\u5b9e\u9645\u529f\u80fd\u53ea\u662f\u7b80\u5355\u7684\u6e05\u7a7a\u8be5\u76ee\u5f55\u4e0b\u7684messag [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[155],"class_list":["post-3699","post","type-post","status-publish","format-standard","hentry","category-shell","tag-shell"],"views":5540,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=3699"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3699\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3699"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}