{"id":5501,"date":"2024-11-02T21:11:04","date_gmt":"2024-11-02T13:11:04","guid":{"rendered":"http:\/\/www.menglanglang.cn\/?p=5501"},"modified":"2024-11-02T21:11:06","modified_gmt":"2024-11-02T13:11:06","slug":"linux-%e4%b8%8b%e6%89%a7%e8%a1%8c%e5%ae%9a%e6%97%b6%e4%bb%bb%e5%8a%a1%e4%b9%8b-systemd-timers","status":"publish","type":"post","link":"http:\/\/www.menglanglang.cn\/?p=5501","title":{"rendered":"Linux \u4e0b\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\u4e4b Systemd Timers"},"content":{"rendered":"\n<p>\u4e0d\u77e5\u9053 ECS \u56e0\u4e3a\u4ec0\u4e48\u7f18\u6545\uff0c\u4e0a\u9762\u5b89\u88c5\u7684 MySQL \u670d\u52a1\u8001\u662f\u4e0d\u5b9a\u671f\u6302\u6389\uff0c\u672c\u6765\u60f3\u901a\u8fc7 Linux \u5f97 Cron \u914d\u7f6e\u4e2a\u534a\u5c0f\u65f6\u7684\u5b9a\u65f6\u68c0\u6d4b\u4efb\u52a1\uff0c\u7ed3\u679c\u4e00\u76f4\u6ca1\u6709\u6267\u884c\uff0c\u56e0\u6b64\u53c8\u5c1d\u8bd5\u4f7f\u7528\u4e86&nbsp;Systemd Timers \u8fdb\u884c\u4e86\u91cd\u65b0\u914d\u7f6e\uff0c\u7b80\u8981\u505a\u4e2a\u8bb0\u5f55\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Systemd Timers \u662f\u73b0\u4ee3 Linux \u7cfb\u7edf\u4e2d CRON \u7684\u4e00\u4e2a\u5f3a\u5927\u66ff\u4ee3\u54c1\u3002\u5b83\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<br \/>1. \u66f4\u7cbe\u786e\u7684\u65f6\u95f4\u63a7\u5236\uff0c\u652f\u6301\u6beb\u79d2\u7ea7\u7cbe\u5ea6\u3002<br \/>2. \u66f4\u597d\u7684\u65e5\u5fd7\u7ba1\u7406\u548c\u9519\u8bef\u62a5\u544a\u3002<br \/>3. \u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u9519\u8fc7\u7684\u4efb\u52a1\uff08\u4f8b\u5982\uff0c\u7cfb\u7edf\u5173\u673a\u65f6\uff09\u3002<br \/>4. \u4e0e\u5176\u4ed6 systemd \u670d\u52a1\u96c6\u6210\u66f4\u597d\u3002<\/p>\n\n\n\n<p>Systemd Timers \u7531\u4e24\u4e2a\u4e3b\u8981\u7ec4\u4ef6\u7ec4\u6210\uff1a<br \/>1. Timer \u5355\u5143\uff08.timer \u6587\u4ef6\uff09\uff1a\u5b9a\u4e49\u4f55\u65f6\u89e6\u53d1\u4efb\u52a1\u3002<br \/>2. Service \u5355\u5143\uff08.service \u6587\u4ef6\uff09\uff1a\u5b9a\u4e49\u8981\u6267\u884c\u7684\u5b9e\u9645\u4efb\u52a1\u3002<br \/>\u5f53 Timer \u5355\u5143\u89e6\u53d1\u65f6\uff0c\u5b83\u4f1a\u542f\u52a8\u76f8\u5e94\u7684 Service \u5355\u5143\uff0c\u8fd9\u79cd\u5206\u79bb\u5141\u8bb8\u66f4\u7075\u6d3b\u7684\u914d\u7f6e\u548c\u7ba1\u7406\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><img loading=\"lazy\" decoding=\"async\" height=\"289\" width=\"900\" src=\"\/\/images.weserv.nl\/?url=https:\/\/i-blog.csdnimg.cn\/direct\/c7972d3f08544c9d90327a9a2666aa96.png\" alt=\"\"\/><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e00\u6b65\uff0c\u521b\u5efa\u9700\u8981\u6267\u884c\u7684\u811a\u672c\u6587\u4ef6\u3002<\/h3>\n\n\n\n<p>\u8fd9\u91cc\u76ee\u7684\u662f\u8981\u68c0\u6d4b MySQL \u670d\u52a1\u662f\u5426\u542f\u52a8\uff0c\u5982\u679c\u672a\u542f\u52a8\uff0c\u5219\u5c1d\u8bd5\u91cd\u542f\u5e76\u518d\u6b21\u68c0\u6d4b\uff0c\u811a\u672c\u8def\u5f84\u6bd4\u5982\u5728 <strong>\/home\/shell\/mysql_restart.sh<\/strong> \u4e0b\uff0c\u5185\u5bb9\u53c2\u8003\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# \u68c0\u67e5MySQL\u8fdb\u7a0b\u662f\u5426\u5728\u8fd0\u884c\necho \"\u68c0\u67e5MySQL\u8fdb\u7a0b\u662f\u5426\u5728\u8fd0\u884c...\" &gt;&gt; mysql_restart.log\nif ! pgrep -x \"mysqld\" &gt; \/dev\/null ; then\n    echo \"MySQL\u8fdb\u7a0b\u672a\u8fd0\u884c\uff0c\u6b63\u5728\u5c1d\u8bd5\u91cd\u542f\u670d\u52a1...\" &gt;&gt; mysql_restart.log\n    systemctl restart mysqld\n    echo \"MySQL\u91cd\u542f\u547d\u4ee4\u5df2\u6267\u884c\uff0c\u63a5\u4e0b\u6765\u8fdb\u5165\u7761\u7720\u7b49\u5f85...\" &gt;&gt; mysql_restart.log\n\n    # \u7a0d\u7b49\u4e00\u4f1a\u513f\u518d\u68c0\u67e5\n    sleep 5\n\n    # \u518d\u6b21\u68c0\u67e5MySQL\u8fdb\u7a0b\n    echo \"\u7761\u77205\u79d2\u540e\u518d\u6b21\u68c0\u67e5MySQL\u8fdb\u7a0b\u662f\u5426\u5728\u8fd0\u884c...\" &gt;&gt; mysql_restart.log\n    if pgrep -x \"mysqld\" &gt; \/dev\/null ; then\n        echo \"MySQL\u670d\u52a1\u91cd\u542f\u6210\u529f\uff0c\u5f53\u524d\u8fd0\u884c\u4e2d\u3002\" &gt;&gt; mysql_restart.log\n    else\n        echo \"MySQL\u670d\u52a1\u91cd\u542f\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u65e5\u5fd7\u4ee5\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002\" &gt;&gt; mysql_restart.log\n    fi\nelse\n    echo \"MySQL\u8fdb\u7a0b\u6b63\u5728\u8fd0\u884c\u3002\" &gt;&gt; mysql_restart.log\nfi<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e8c\u6b65\uff0c\u521b\u5efa Service \u5355\u5143\u3002<\/h3>\n\n\n\n<p>\u5728\u8def\u5f84&nbsp;<strong>\/etc\/systemd\/system<\/strong> \u4e0b\uff0c\u521b\u5efa\u9700\u8981\u6267\u884c\u811a\u672c\u7684&nbsp;Service \u5355\u5143\uff0c\u6bd4\u5982&nbsp;<strong>mysql_restart.service<\/strong>\uff0c\u5185\u5bb9\u53c2\u8003\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Unit]\nDescription=\u5b9a\u65f6\u8fd0\u884cMySQL\u91cd\u542f\u811a\u672c\n\n&#91;Service]\nType=simple\nExecStart=\/home\/shell\/mysql_restart.sh<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e09\u6b65\uff0c\u521b\u5efa Timer \u5355\u5143\u3002<\/h3>\n\n\n\n<p>\u8fd8\u662f\u5728\u8def\u5f84&nbsp;<strong>\/etc\/systemd\/system<\/strong> \u4e0b\uff0c\u521b\u5efa\u9700\u8981\u6267\u884c\u670d\u52a1\u7684&nbsp;Timer \u5355\u5143\uff0c\u6bd4\u5982&nbsp;<strong>mysql_restart.timer<\/strong>\uff0c\u5185\u5bb9\u53c2\u8003\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Unit]\nDescription=\u5b9a\u65f6\u8fd0\u884cMySQL\u91cd\u542f\u811a\u672c\u670d\u52a1\n\n&#91;Timer]\n# OnCalendar=houry\n# Persistent=true\nOnBootSec=1min\nOnUnitActiveSec=30min\n\n&#91;Install]\nWantedBy=timers.target<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0cOnBootSec \u8868\u793a\u7cfb\u7edf\u91cd\u542f\u540e\u591a\u4e45\u5f00\u59cb\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\uff1bOnUnitActiveSec \u8868\u793a\u5f53\u670d\u52a1\u5355\u5143\u8fdb\u5165\u6d3b\u52a8\u72b6\u6001\u65f6\u591a\u4e45\u6267\u884c\u4e00\u6b21\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u56db\u6b65\uff0c\u52a0\u8f7d\u670d\u52a1\u5e76\u542f\u52a8\u5b9a\u65f6\u4efb\u52a1\u3002<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>systemctl daemon-reload<\/p>\n\n\n\n<p>systemctl enable mysql_restart.timer<\/p>\n\n\n\n<p>systemctl start&nbsp;mysql_restart.timer<\/p>\n<\/blockquote>\n\n\n\n<p>\u5f53\u7136 Cron \u5b9a\u65f6\u4efb\u52a1\u8fd8\u6709\u5f88\u591a\u5176\u5b83\u7684\u66ff\u4ee3\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u6bd4\u5982&nbsp;Anacron\u3001at \u547d\u4ee4\u7b49\uff0c\u53ef\u4ee5\u81ea\u884c\u5c1d\u8bd5\uff0c\u8fd9\u91cc\u5c31\u4e0d\u7ec6\u5c55\u5f00\u8bf4\u4e86\uff0cGood Luck\uff01 \u200b<\/p>\n\n\n\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/menglanglang.blog.csdn.net\/article\/details\/143456133\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/menglanglang.blog.csdn.net\/article\/details\/143456133<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0d\u77e5\u9053 ECS \u56e0\u4e3a\u4ec0\u4e48\u7f18\u6545\uff0c\u4e0a\u9762\u5b89\u88c5\u7684 MySQL \u670d\u52a1\u8001\u662f\u4e0d\u5b9a\u671f\u6302\u6389\uff0c\u672c\u6765\u60f3\u901a\u8fc7 Linux \u5f97 Cron [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[227],"tags":[354,602,186,349,939,695,848,911],"class_list":["post-5501","post","type-post","status-publish","format-standard","hentry","category-linux","tag-centos","tag-linux","tag-mysql","tag-shell","tag-939","tag-695","tag-848","tag-911"],"_links":{"self":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/5501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5501"}],"version-history":[{"count":1,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/5501\/revisions"}],"predecessor-version":[{"id":5502,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/5501\/revisions\/5502"}],"wp:attachment":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5501"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}