{"id":217589,"date":"2021-05-16T10:25:47","date_gmt":"2021-05-16T02:25:47","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=217589"},"modified":"2021-04-28T10:26:30","modified_gmt":"2021-04-28T02:26:30","slug":"shell-linux-good","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/shell-linux-good.html","title":{"rendered":"\u5927\u795e\u6559\u4f60\u5982\u4f55\u7ed9\u811a\u672c\u5199\u4e00\u4e2a\u5b88\u62a4\u8fdb\u7a0b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u6211\u4eec\u65e5\u5e38\u8fd0\u7ef4\u4e2d\uff0c\u5199\u811a\u672c\u76d1\u63a7\u4e00\u4e2a\u8fdb\u7a0b\u662f\u6bd4\u8f83\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u6211\u8981\u76d1\u63a7mysql\u8fdb\u7a0b\u662f\u5426\u6d88\u5931\uff0c\u5982\u679c\u6d88\u5931\u5c31\u91cd\u542fmysql\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u7528\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u5c31\u53ef\u4ee5\u5b9e\u73b0\uff1a<\/p>\n

#!\/bin\/sh\r\n\r\nDate=` date \u2018+%c\u2019`\r\n\r\nwhile :\r\n\r\ndo\r\n\r\nif ! ps aux | grep -w mysqld | grep -v grep >\/dev\/null 2>&1\r\n\r\nthen\r\n\r\n\/etc\/init.d\/mysqld start\r\n\r\necho $Date mysqld was reboot >>\/var\/log\/reboot_mysql.log\r\n\r\nfi\r\n\r\ndone<\/pre>\n

\u4f46\u5982\u679c\u76d1\u63a7\u811a\u672c\u672c\u8eab\u51fa\u4e86\u95ee\u9898\uff0c\u5c31\u65e0\u6cd5\u6309\u6211\u4eec\u8981\u6c42\u542f\u52a8\u7a0b\u5e8f\u4e86\uff0c\u8fd9\u91cc\u8fd9\u662f\u4ee5mysql\u4e3a\u4f8b\u5b50\uff0c\u4f46\u5b9e\u9645\u4e2d\u5982\u679c\u662f\u8d1f\u8d23\u62a5\u8b66\u7684\u811a\u672c\u51fa\u4e86\u95ee\u9898\uff0c\u62a5\u8b66\u6ca1\u53d1\u51fa\u6765\uff0c\u90a3\u5c31\u6bd4\u8f83\u5c34\u5c2c\u4e86\uff0c\u6240\u4ee5\u4e3a\u4fdd\u8bc1\u6211\u4eec\u7684\u68c0\u67e5\u811a\u672c\u80fd\u5b9e\u65f6\u8fd0\u884c\uff0c\u6211\u4eec\u6709\u65f6\u9700\u8981\u5199\u4e00\u4e2a\u5b88\u62a4\u8fdb\u7a0b\uff08\u5f53\u7136\u4e0d\u6b62\u811a\u672c\uff0c\u7cfb\u7edf\u4e2d\u7684\u4efb\u4f55\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u9760\u5b88\u62a4\u8fdb\u7a0b\u542f\u52a8\uff09\uff0c\u8fd9\u5c31\u662f\u6211\u4eec\u4eca\u5929\u8981\u8bf4\u7684\u4e3b\u9898\uff0c\u5982\u4f55\u7ed9\u811a\u672c\u5199\u4e00\u4e2adaemon\u8fdb\u7a0b\uff0c\u6211\u4eec\u5148\u4e0a\u4ee3\u7801\uff1a<\/p>\n

#!\/usr\/bin\/python\r\n\r\nimport subprocess\r\n\r\nfrom daemon import runner\r\n\r\ncmd = \u201c\/root\/demo_script\/restart_mysql.sh\u201d\r\n\r\nclass App():\r\n\r\ndef __init__(self):\r\n\r\nself.stdin_path = \u2018\/dev\/null\u2019\r\n\r\nself.stdout_path = \u2018\/dev\/tty\u2019\r\n\r\nself.stderr_path = \u2018\/dev\/tty\u2019\r\n\r\nself.pidfile_path =  \u2018\/tmp\/hello.pid\u2019\r\n\r\nself.pidfile_timeout = 5\r\n\r\ndef start_subprocess(self):\r\n\r\nreturn subprocess.Popen(cmd, shell=True)\r\n\r\ndef run(self):\r\n\r\np = self.start_subprocess()\r\n\r\nwhile True:\r\n\r\nres = p.poll()\r\n\r\nif res is not None:\r\n\r\np = self.start_subprocess()\r\n\r\nif __name__ == \u2018__main__\u2019:\r\n\r\napp = App()\r\n\r\ndaemon_runner = runner.DaemonRunner(app)\r\n\r\ndaemon_runner.do_action()<\/pre>\n

\u811a\u672c\u6bd4\u8f83\u7b80\u5355\uff0c\u6ca1\u4ec0\u4e48\u7279\u522b\u7684\u903b\u8f91\uff0c\u5173\u4e8edaemon\u8fd9\u4e2a\u6a21\u5757\u5982\u4f55\u4f7f\u7528\uff0c\u6211\u8fd9\u91cc\u7ed9\u51fa\u4e00\u6bb5\u5b98\u65b9\u7684\u89e3\u91ca\uff0c\u5199\u7684\u975e\u5e38\u660e\u767d\uff0c\u6ce8\u610f\u54df\uff0c\u662f\u82f1\u6587\u7684\uff0c\u5728\u8fd9\u6211\u5c31\u4e0d\u7ffb\u8bd1\u4e86\uff0c\u5982\u679c\u4e0d\u7406\u89e3\u5c31\u67e5\u67e5\u5b57\u5178\uff0c\u5c31\u5f53\u591a\u5b66\u51e0\u4e2a\u5355\u8bcd\u4e86\u5427\u3002<\/p>\n

__init__(self, app)\r\n\r\n|      Set up the parameters of a new runner.\r\n\r\n|\r\n\r\n|      The `app` argument must have the following attributes:\r\n\r\n|\r\n\r\n|      * `stdin_path`, `stdout_path`, `stderr_path`: Filesystem\r\n\r\n|        paths to open and replace the existing `sys.stdin`,\r\n\r\n|        `sys.stdout`, `sys.stderr`.\r\n\r\n|\r\n\r\n|      * `pidfile_path`: Absolute filesystem path to a file that\r\n\r\n|        will be used as the PID file for the daemon. If\r\n\r\n|        \u201cNone\u201c, no PID file will be used.\r\n\r\n|\r\n\r\n|      * `pidfile_timeout`: Used as the default acquisition\r\n\r\n|        timeout value supplied to the runner\u2019s PID lock file.\r\n\r\n|\r\n\r\n|      * `run`: Callable that will be invoked when the daemon is\r\n\r\n|        started.\r\n\r\n|\r\n\r\n|  do_action(self)\r\n\r\n|      Perform the requested action.\r\n\r\n|\r\n\r\n|  parse_args(self, argv=None)\r\n\r\n|      Parse command-line arguments.<\/pre>\n

\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\uff0c\u5b88\u62a4\u8fdb\u7a0b\u7684\u542f\u52a8\u6bd4\u8f83\u9ad8\u5927\u4e0a\uff0c\u8f93\u5165\u4ee5\u4e0a\u4ee3\u7801\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u7ec8\u7aef\u8f93\u5165\uff1a<\/p>\n

#python monitor.py  start<\/pre>\n

\u5f53\u7136\u8fd8\u6709stop,restart\u7b49\u53c2\u6570\u3002<\/p>\n

\u8fd9\u91cc\u6211\u4ecb\u7ecd\u7684\u662f\u5176\u4e2d\u4e00\u4e2a\u5e94\u7528\u573a\u666f\uff0c\u5b9e\u9645\u4e2d\u53ef\u4ee5\u7075\u6d3b\u8fd0\u7528\uff0c\u6bd4\u59821\u53f0\u670d\u52a1\u5668\u4e0a\u542f\u52a8\u7684\u7a0b\u5e8f\u8fc7\u591a\uff0c\u73af\u5883\u914d\u7f6e\u6bd4\u8f83\u590d\u6742\uff0c\u5c31\u53ef\u4ee5\u5148\u542f\u52a8daemon\u8fdb\u7a0b\uff0c\u7136\u540e\u901a\u8fc7daemon\u6765\u542f\u52a8\u5176\u5b83\u6240\u6709\u5e94\u7528\u7a0b\u5e8f\uff0c\u5c31\u4e0d\u7528\u4e00\u4e2a\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u4e86\uff0c\u800c\u4e14\u8fd8\u80fd\u8d77\u5230\u5b9e\u65f6\u76d1\u63a7\u7684\u4f5c\u7528\uff0c\u5f88\u65b9\u4fbf\u5427\uff0c\u8fd9\u7bc7\u5c31\u5230\u8fd9\u91cc\uff0c\u6709\u95ee\u9898\u53ef\u4ee5\u7ed9\u6211\u7559\u8a00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u7528\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u5c31\u53ef\u4ee5\u5b9e\u73b0\uff1a #!\/bin\/sh Date=` date \u2018+%c\u2019` while : do i […]<\/p>\n","protected":false},"author":1482,"featured_media":208904,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-217589","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/217589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=217589"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/217589\/revisions"}],"predecessor-version":[{"id":217592,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/217589\/revisions\/217592"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/208904"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=217589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=217589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=217589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}