{"id":17124,"date":"2024-11-18T16:10:25","date_gmt":"2024-11-18T08:10:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/17124\/"},"modified":"2024-11-18T16:10:25","modified_gmt":"2024-11-18T08:10:25","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8systemd%e5%92%8ccrontab%e5%9c%a8linux%e7%b3%bb%e7%bb%9f%e4%b8%ad%e5%ae%9a%e6%97%b6%e6%89%a7%e8%a1%8c%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/17124\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Systemd\u548cCrontab\u5728Linux\u7cfb\u7edf\u4e2d\u5b9a\u65f6\u6267\u884c\u811a\u672c"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169571002542499.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Systemd\u548cCrontab\u5728Linux\u7cfb\u7edf\u4e2d\u5b9a\u65f6\u6267\u884c\u811a\u672c\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Systemd\u548cCrontab\u5728Linux\u7cfb\u7edf\u4e2d\u5b9a\u65f6\u6267\u884c\u811a\u672c\u63d2\u56fe\" \/><\/p>\n<p>\u6807\u9898\uff1a\u4f7f\u7528Systemd\u548cCrontab\u5728Linux\u7cfb\u7edf\u4e2d\u5b9a\u65f6\u6267\u884c\u811a\u672c<\/p>\n<p>\u6b63\u6587\uff1a<br \/>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5b9a\u65f6\u6267\u884c\u4e00\u4e9b\u811a\u672c\u4efb\u52a1\uff0c\u6bd4\u5982\u5907\u4efd\u6570\u636e\u3001\u6e05\u7406\u65e5\u5fd7\u7b49\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Systemd\u548cCrontab\u4e24\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u5b9a\u65f6\u6267\u884c\u811a\u672c\u7684\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u4f7f\u7528Systemd\u5b9a\u65f6\u6267\u884c\u811a\u672c<\/p>\n<p>Systemd\u662fLinux\u7cfb\u7edf\u4e0b\u7684\u4e00\u4e2a\u521d\u59cb\u5316\u7cfb\u7edf\u548c\u670d\u52a1\u7ba1\u7406\u5668\uff0c\u901a\u8fc7\u521b\u5efaSystemd\u5b9a\u65f6\u4efb\u52a1\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9a\u65f6\u6267\u884c\u811a\u672c\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a.service\u6587\u4ef6\uff0c\u7528\u4e8e\u5b9a\u4e49\u5b9a\u65f6\u4efb\u52a1\u7684\u76f8\u5173\u53c2\u6570\u3002\u4ee5\u5b9a\u65f6\u6e05\u7406\u65e5\u5fd7\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3acleanuplog.service\u7684\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<pre>[Unit]\nDescription=Cleanup Log Service\n\n[Service]\nType=simple\nExecStart=\/path\/to\/cleanuplog.sh\n\n[Install]\nWantedBy=default.target<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0cExecStart\u6307\u5b9a\u4e86\u8981\u6267\u884c\u7684\u811a\u672c\u8def\u5f84\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539\u3002\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a.timer\u6587\u4ef6\uff0c\u7528\u4e8e\u5b9a\u4e49\u5b9a\u65f6\u4efb\u52a1\u7684\u89e6\u53d1\u6761\u4ef6\u548c\u6267\u884c\u903b\u8f91\u3002\u4ee5\u6bcf\u5929\u51cc\u66681\u70b9\u6267\u884c\u4e3a\u4f8b\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3acleanuplog.timer\u7684\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<pre>[Unit]\nDescription=Cleanup Log Timer\n\n[Timer]\nOnCalendar=*-*-* 01:00:00\n\n[Install]\nWantedBy=timers.target<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0cOnCalendar\u6307\u5b9a\u4e86\u5b9a\u65f6\u4efb\u52a1\u7684\u89e6\u53d1\u65f6\u95f4\uff0c\u8fd9\u91cc\u662f\u6bcf\u5929\u51cc\u66681\u70b9\u3002\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<ol>\n<li>\u5c06\u4e24\u4e2a\u6587\u4ef6\u79fb\u52a8\u5230\/etc\/systemd\/system\u76ee\u5f55\u4e0b\u3002<\/li>\n<\/ol>\n<pre>$ sudo mv cleanuplog.service \/etc\/systemd\/system\n$ sudo mv cleanuplog.timer \/etc\/systemd\/system<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u542f\u7528\u5e76\u542f\u52a8\u5b9a\u65f6\u4efb\u52a1\u3002<\/li>\n<\/ol>\n<pre>$ sudo systemctl daemon-reload\n$ sudo systemctl enable cleanuplog.timer\n$ sudo systemctl start cleanuplog.timer<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u5b9a\u65f6\u6267\u884c\u811a\u672c\u7684\u4efb\u52a1\u5c31\u5df2\u7ecf\u521b\u5efa\u5e76\u542f\u52a8\u6210\u529f\u4e86\u3002<\/p>\n<p>\u4e8c\u3001\u4f7f\u7528Crontab\u5b9a\u65f6\u6267\u884c\u811a\u672c<\/p>\n<p>Crontab\u662f\u4e00\u79cd\u7528\u4e8e\u5468\u671f\u6027\u6267\u884c\u547d\u4ee4\u6216\u811a\u672c\u7684\u5de5\u5177\uff0c\u901a\u8fc7\u7f16\u8f91Crontab\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9a\u5236\u5b9a\u65f6\u6267\u884c\u811a\u672c\u7684\u9700\u6c42\u3002<\/p>\n<ol>\n<li>\u7f16\u8f91\u5f53\u524d\u7528\u6237\u7684Crontab\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<pre>$ crontab -e<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5728\u6253\u5f00\u7684\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5b9a\u65f6\u6267\u884c\u811a\u672c\u7684\u547d\u4ee4\u3002\u4ee5\u6bcf\u5929\u51cc\u66682\u70b9\u6267\u884c\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u6dfb\u52a0\u5982\u4e0b\u5185\u5bb9\u3002<\/li>\n<\/ol>\n<pre>0 2 * * * \/path\/to\/cleanuplog.sh<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0c0 2 * * *\u8868\u793a\u6bcf\u5929\u51cc\u66682\u70b9\uff0c\/path\/to\/cleanuplog.sh\u8868\u793a\u8981\u6267\u884c\u7684\u811a\u672c\u8def\u5f84\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539\u3002<\/p>\n<ol>\n<li>\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p>\u73b0\u5728\uff0c\u5b9a\u65f6\u6267\u884c\u811a\u672c\u7684\u4efb\u52a1\u5c31\u6dfb\u52a0\u6210\u529f\u4e86\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<br \/>\u672c\u6587\u4ecb\u7ecd\u4e86\u5728Linux\u7cfb\u7edf\u4e2d\u4f7f\u7528Systemd\u548cCrontab\u4e24\u79cd\u65b9\u5f0f\u6765\u5b9a\u65f6\u6267\u884c\u811a\u672c\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e86\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u81ea\u52a8\u5316\u7ba1\u7406\u80fd\u529b\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u5b66\u4e60\u548c\u5e94\u7528\u5b9a\u65f6\u6267\u884c\u811a\u672c\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Systemd\u548cCrontab\u5728Linux\u7cfb\u7edf\u4e2d\u5b9a\u65f6\u6267\u884c\u811a\u672c\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6807\u9898\uff1a\u4f7f\u7528Systemd\u548cCrontab\u5728Linux\u7cfb\u7edf\u4e2d\u5b9a\u65f6\u6267\u884c\u811a\u672c \u6b63\u6587\uff1a\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5b9a\u65f6\u6267\u884c\u4e00\u4e9b\u811a\u672c\u4efb\u52a1\uff0c\u6bd4\u5982\u5907\u4efd\u6570\u636e\u3001\u6e05\u7406\u65e5\u5fd7\u7b49\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Systemd\u548cCrontab\u4e24\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u5b9a\u65f6\u6267\u884c\u811a\u672c\u7684\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u4f7f\u7528Systemd\u5b9a\u65f6\u6267\u884c\u811a\u672c Systemd\u662fLinux\u7cfb\u7edf\u4e0b\u7684\u4e00\u4e2a\u521d\u59cb\u5316\u7cfb\u7edf\u548c\u670d\u52a1\u7ba1\u7406\u5668\uff0c\u901a\u8fc7\u521b\u5efaSystemd\u5b9a\u65f6\u4efb\u52a1\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9a\u65f6\u6267\u884c\u811a\u672c\u3002 \u521b\u5efa\u4e00\u4e2a.service\u6587\u4ef6\uff0c\u7528\u4e8e\u5b9a\u4e49\u5b9a\u65f6\u4efb\u52a1\u7684\u76f8\u5173\u53c2\u6570\u3002\u4ee5\u5b9a\u65f6\u6e05\u7406\u65e5\u5fd7\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3acleanuplog.service\u7684\u6587\u4ef6\u3002 [Unit] Description=Cleanup Log Service [Service] Type=simple ExecStart=\/path\/to\/cleanuplog.sh [Install] WantedBy=default.target \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0cExecStart\u6307\u5b9a\u4e86\u8981\u6267\u884c\u7684\u811a\u672c\u8def\u5f84\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539\u3002\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u3002 \u521b\u5efa\u4e00\u4e2a.timer\u6587\u4ef6\uff0c\u7528\u4e8e\u5b9a\u4e49\u5b9a\u65f6\u4efb\u52a1\u7684\u89e6\u53d1\u6761\u4ef6\u548c\u6267\u884c\u903b\u8f91\u3002\u4ee5\u6bcf\u5929\u51cc\u66681\u70b9\u6267\u884c\u4e3a\u4f8b\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3acleanuplog.timer\u7684\u6587\u4ef6\u3002 [Unit] Description=Cleanup Log Timer [Timer] OnCalendar=*-*-* 01:00:00 [Install] WantedBy=timers.target \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0cOnCalendar\u6307\u5b9a\u4e86\u5b9a\u65f6\u4efb\u52a1\u7684\u89e6\u53d1\u65f6\u95f4\uff0c\u8fd9\u91cc\u662f\u6bcf\u5929\u51cc\u66681\u70b9\u3002\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u3002 \u5c06\u4e24\u4e2a\u6587\u4ef6\u79fb\u52a8\u5230\/etc\/systemd\/system\u76ee\u5f55\u4e0b\u3002 $ sudo mv cleanuplog.service \/etc\/systemd\/system $ sudo mv cleanuplog.timer \/etc\/systemd\/system \u767b\u5f55\u540e\u590d\u5236 \u542f\u7528\u5e76\u542f\u52a8\u5b9a\u65f6\u4efb\u52a1\u3002 $ sudo systemctl daemon-reload $ sudo systemctl enable cleanuplog.timer $ sudo systemctl start cleanuplog.timer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-17124","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=17124"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17124\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=17124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=17124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=17124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}