{"id":795,"date":"2024-11-07T14:27:04","date_gmt":"2024-11-07T06:27:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/795\/"},"modified":"2024-11-07T14:27:04","modified_gmt":"2024-11-07T06:27:04","slug":"python%e6%80%8e%e4%b9%88%e5%ae%9a%e6%97%b6%e5%90%af%e5%8a%a8%e7%88%ac%e8%99%ab","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/795\/","title":{"rendered":"python\u600e\u4e48\u5b9a\u65f6\u542f\u52a8\u722c\u866b"},"content":{"rendered":"<blockquote><p>\n  \u60a8\u53ef\u4ee5\u4f7f\u7528 crontab \u5728\u7279\u5b9a\u65f6\u95f4\u6bb5\u81ea\u52a8\u542f\u52a8 python \u722c\u866b\u3002\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa python \u722c\u866b\u811a\u672c\u5e76\u786e\u4fdd\u5176\u53ef\u6267\u884c\u3002\u6253\u5f00 crontab \u5e76\u6dfb\u52a0\u4e00\u884c\uff0c\u683c\u5f0f\u4e3a * \/path\/to\/script.py\u3002\u8bbe\u7f6e\u65f6\u95f4\u95f4\u9694\uff08\u4f8b\u5982\uff0c\u6bcf\u5929\u51cc\u6668 3 \u70b9\uff09\u3002\u4fdd\u5b58\u5e76\u9000\u51fa crontab\uff0c\u6309 y \u786e\u8ba4\u4fdd\u5b58\u3002\n<\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1109\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101815482232851.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101815482232851.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101815482232851-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101815482232851-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101815482232851-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"python\u600e\u4e48\u5b9a\u65f6\u542f\u52a8\u722c\u866b\u63d2\u56fe\" alt=\"python\u600e\u4e48\u5b9a\u65f6\u542f\u52a8\u722c\u866b\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5b9a\u65f6\u542f\u52a8 Python \u722c\u866b<\/strong><\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528 crontab \u5728\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u5185\u81ea\u52a8\u542f\u52a8 Python \u722c\u866b\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u722c\u866b\u811a\u672c\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a Python \u811a\u672c\uff0c\u5305\u542b\u60a8\u7684\u722c\u866b\u4ee3\u7801\u3002<\/li>\n<li>\u786e\u4fdd\u811a\u672c\u5177\u6709\u53ef\u6267\u884c\u6743\u9650\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6253\u5f00 crontab\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165 crontab -e\u3002<\/li>\n<li>\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f Debian \u6216 Ubuntu\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 sudo crontab -e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0 cron \u4f5c\u4e1a\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u5728 crontab \u6587\u4ef6\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u884c\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre>* * * * * \/path\/to\/script.py<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u66ff\u6362 \/path\/to\/script.py \u4e3a\u722c\u866b\u811a\u672c\u7684\u8def\u5f84\u3002<\/li>\n<li>\u661f\u53f7 (*) \u8868\u793a\u8be5\u4f5c\u4e1a\u5728\u6bcf\u4e2a\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u65f6\u95f4\u95f4\u9694\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u8981\u8bbe\u7f6e\u4e0d\u540c\u7684\u65f6\u95f4\u95f4\u9694\uff0c\u8bf7\u4fee\u6539 crontab \u8868\u8fbe\u5f0f\u4e2d\u7684\u5b57\u6bb5\u3002<\/p>\n<ul>\n<li>\u5206\u949f (0-59)<\/li>\n<li>\u5c0f\u65f6 (0-23)<\/li>\n<li>\u661f\u671f\u51e0 (0-6\uff0c0=\u661f\u671f\u65e5)<\/li>\n<li>\u6708\u4efd (1-12)<\/li>\n<li>\u661f\u671f\u51e0 (0-6\uff0c0=\u661f\u671f\u65e5)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4fdd\u5b58 crontab\uff1a<\/strong><\/p>\n<ul>\n<li>\u6309 Ctrl+X \u4fdd\u5b58\u5e76\u9000\u51fa crontab\u3002<\/li>\n<li>\u6309 Y \u786e\u8ba4\u4fdd\u5b58\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8981\u5728\u6bcf\u5929\u51cc\u6668 3 \u70b9\u542f\u52a8\u722c\u866b\uff0c\u8bf7\u6dfb\u52a0\u4ee5\u4e0b\u884c\u5230 crontab \u6587\u4ef6\u4e2d\uff1a<\/p>\n<pre>0 3 * * * \/path\/to\/script.py<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd Python \u5df2\u5b89\u88c5\u5728\u60a8\u7684\u7cfb\u7edf\u4e0a\u3002<\/li>\n<li>\u60a8\u7684\u722c\u866b\u811a\u672c\u5fc5\u987b\u80fd\u591f\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u3002<\/li>\n<li>crontab \u4f5c\u4e1a\u4ec5\u5728\u60a8\u767b\u5f55\u65f6\u624d\u8fd0\u884c\uff0c\u56e0\u6b64\u60a8\u53ef\u80fd\u9700\u8981\u4f7f\u7528 nohup \u547d\u4ee4\u5c06\u5176\u4f5c\u4e3a\u540e\u53f0\u8fdb\u7a0b\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fpython\u600e\u4e48\u5b9a\u65f6\u542f\u52a8\u722c\u866b\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>\u60a8\u53ef\u4ee5\u4f7f\u7528 crontab \u5728\u7279\u5b9a\u65f6\u95f4\u6bb5\u81ea\u52a8\u542f\u52a8 python \u722c\u866b\u3002\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa python \u722c\u866b\u811a\u672c\u5e76\u786e\u4fdd\u5176\u53ef\u6267\u884c\u3002\u6253\u5f00 crontab \u5e76\u6dfb\u52a0\u4e00\u884c\uff0c\u683c\u5f0f\u4e3a * \/path\/to\/script.py\u3002\u8bbe\u7f6e\u65f6\u95f4\u95f4\u9694\uff08\u4f8b\u5982\uff0c\u6bcf\u5929\u51cc\u6668 3 \u70b9\uff09\u3002\u4fdd\u5b58\u5e76\u9000\u51fa crontab\uff0c\u6309 y \u786e\u8ba4\u4fdd\u5b58\u3002 \u5982\u4f55\u5b9a\u65f6\u542f\u52a8 Python \u722c\u866b \u56de\u7b54\uff1a\u60a8\u53ef\u4ee5\u4f7f\u7528 crontab \u5728\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u5185\u81ea\u52a8\u542f\u52a8 Python \u722c\u866b\u3002 \u8be6\u7ec6\u6b65\u9aa4\uff1a \u521b\u5efa\u722c\u866b\u811a\u672c\uff1a \u7acb\u5373\u5b66\u4e60\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b \u521b\u5efa\u4e00\u4e2a Python \u811a\u672c\uff0c\u5305\u542b\u60a8\u7684\u722c\u866b\u4ee3\u7801\u3002 \u786e\u4fdd\u811a\u672c\u5177\u6709\u53ef\u6267\u884c\u6743\u9650\u3002 \u6253\u5f00 crontab\uff1a \u5728\u7ec8\u7aef\u4e2d\u8f93\u5165 crontab -e\u3002 \u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f Debian \u6216 Ubuntu\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 sudo crontab -e\u3002 \u6dfb\u52a0 cron \u4f5c\u4e1a\uff1a \u5728 crontab \u6587\u4ef6\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u884c\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a * * * * * \/path\/to\/script.py [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-795","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/795","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=795"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/795\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}