{"id":1116992,"date":"2025-01-08T18:21:49","date_gmt":"2025-01-08T10:21:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1116992.html"},"modified":"2025-01-08T18:21:52","modified_gmt":"2025-01-08T10:21:52","slug":"%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8a%e5%a6%82%e4%bd%95%e5%ae%9a%e6%97%b6%e6%89%a7%e8%a1%8cpython%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1116992.html","title":{"rendered":"\u670d\u52a1\u5668\u4e0a\u5982\u4f55\u5b9a\u65f6\u6267\u884cpython\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081059\/b998a7b3-d9ec-452c-8d36-416dbcd9a082.webp\" alt=\"\u670d\u52a1\u5668\u4e0a\u5982\u4f55\u5b9a\u65f6\u6267\u884cpython\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u670d\u52a1\u5668\u4e0a\u5b9a\u65f6\u6267\u884cPython\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528cron\u4efb\u52a1\u3001\u7cfb\u7edfd\u8ba1\u5212\u4efb\u52a1\u548c\u4e13\u7528\u8c03\u5ea6\u5e93\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7Linux\u7cfb\u7edf\u7684cron\u4efb\u52a1\u6765\u5b9e\u73b0\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528cron\u4efb\u52a1<\/strong><\/p>\n<p>cron\u662fUnix\u548c\u7c7bUnix\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u4e00\u79cd\u7528\u4e8e\u65f6\u95f4\u8c03\u5ea6\u7684\u5b88\u62a4\u8fdb\u7a0b\u3002\u901a\u8fc7\u7f16\u8f91crontab\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u6267\u884c\u6307\u5b9a\u7684\u547d\u4ee4\u6216\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4ec0\u4e48\u662fcron\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>cron\u662f\u7528\u4e8e\u5728\u7279\u5b9a\u65f6\u95f4\u548c\u65e5\u671f\u6267\u884c\u547d\u4ee4\u7684\u5b88\u62a4\u8fdb\u7a0b\u3002\u5b83\u9002\u7528\u4e8e\u6267\u884c\u5468\u671f\u6027\u4efb\u52a1\uff0c\u5982\u5907\u4efd\u3001\u6e05\u7406\u65e5\u5fd7\u6587\u4ef6\u7b49\u3002cron\u4efb\u52a1\u7531crontab\u6587\u4ef6\u7ba1\u7406\uff0ccrontab\u6587\u4ef6\u4e2d\u5305\u542b\u4e86\u8ba1\u5212\u4efb\u52a1\u7684\u65f6\u95f4\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u914d\u7f6ecron\u4efb\u52a1<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u67e5\u770b\u5f53\u524d\u7528\u6237\u7684crontab\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">crontab -l<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u662f\u7b2c\u4e00\u6b21\u4f7f\u7528crontab\u6587\u4ef6\uff0c\u53ef\u80fd\u4f1a\u63d0\u793a\u201cno crontab for [username]\u201d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u8f91crontab\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">crontab -e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u6253\u5f00\u4e00\u4e2a\u6587\u672c\u7f16\u8f91\u5668\uff0c\u5141\u8bb8\u4f60\u7f16\u8f91\u5f53\u524d\u7528\u6237\u7684crontab\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u8ba1\u5212\u4efb\u52a1<\/strong>\uff1a<\/p>\n<p>\u5728crontab\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u884c\u6765\u6267\u884cPython\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u6bcf\u5929\u65e9\u4e0a3\u70b9\u6267\u884c\u4e00\u4e2aPython\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">0 3 * * * \/usr\/bin\/python3 \/path\/to\/your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u547d\u4ee4\u7684\u683c\u5f0f\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>* * * * * command to be executed<\/p>\n<p>- - - - -<\/p>\n<p>| | | | |<\/p>\n<p>| | | | +---- Day of the week (0 - 7) (Sunday=0 or 7)<\/p>\n<p>| | | +------ Month (1 - 12)<\/p>\n<p>| | +-------- Day of the month (1 - 31)<\/p>\n<p>| +---------- Hour (0 - 23)<\/p>\n<p>+------------ Minute (0 - 59)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u9a8c\u8bc1cron\u4efb\u52a1<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u67e5\u770bcron\u4efb\u52a1\u65e5\u5fd7<\/strong>\uff1a<\/p>\n<p>cron\u4efb\u52a1\u7684\u6267\u884c\u60c5\u51b5\u8bb0\u5f55\u5728\u7cfb\u7edf\u65e5\u5fd7\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u65e5\u5fd7\u6587\u4ef6\u6765\u786e\u8ba4\u4efb\u52a1\u662f\u5426\u6210\u529f\u6267\u884c\u3002\u5e38\u89c1\u7684\u65e5\u5fd7\u6587\u4ef6\u8def\u5f84\u662f\/var\/log\/cron\u6216\/var\/log\/syslog\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">t<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l -f \/var\/log\/cron<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">tail -f \/var\/log\/syslog<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6d4b\u8bd5cron\u4efb\u52a1<\/strong>\uff1a<\/p>\n<p>\u4e3a\u4e86\u786e\u8ba4cron\u4efb\u52a1\u662f\u5426\u914d\u7f6e\u6b63\u786e\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u6d4b\u8bd5\u4efb\u52a1\uff0c\u6bcf\u5206\u949f\u6267\u884c\u4e00\u6b21\uff0c\u7136\u540e\u68c0\u67e5\u65e5\u5fd7\u6587\u4ef6\u786e\u8ba4\u4efb\u52a1\u662f\u5426\u88ab\u6267\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u73af\u5883\u53d8\u91cf<\/strong>\uff1a<\/p>\n<p>cron\u4efb\u52a1\u6267\u884c\u7684\u73af\u5883\u4e0e\u7528\u6237\u7684\u4ea4\u4e92\u5f0fshell\u73af\u5883\u4e0d\u540c\uff0c\u53ef\u80fd\u6ca1\u6709\u52a0\u8f7d\u7528\u6237\u7684\u73af\u5883\u53d8\u91cf\u3002\u56e0\u6b64\uff0c\u5728\u811a\u672c\u4e2d\u9700\u8981\u663e\u5f0f\u8bbe\u7f6e\u5fc5\u8981\u7684\u73af\u5883\u53d8\u91cf\uff0c\u6216\u5728crontab\u6587\u4ef6\u7684\u9876\u90e8\u6dfb\u52a0\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6587\u4ef6\u6743\u9650<\/strong>\uff1a<\/p>\n<p>\u786e\u4fddPython\u811a\u672c\u6709\u6267\u884c\u6743\u9650\uff0c\u53ef\u4ee5\u901a\u8fc7chmod\u547d\u4ee4\u4fee\u6539\u6587\u4ef6\u6743\u9650\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">chmod +x \/path\/to\/your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528cron\u4efb\u52a1\u5b9a\u65f6\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u5199Python\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672ctest_script.py\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">#!\/usr\/bin\/env python3<\/p>\n<p>import datetime<\/p>\n<p>def main():<\/p>\n<p>    with open(&#39;\/path\/to\/log.txt&#39;, &#39;a&#39;) as f:<\/p>\n<p>        f.write(f&#39;Cron job executed at {datetime.datetime.now()}\\n&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u914d\u7f6ecron\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u6253\u5f00crontab\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">crontab -e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff0c\u6bcf\u5929\u65e9\u4e0a3\u70b9\u6267\u884c\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">0 3 * * * \/usr\/bin\/python3 \/path\/to\/test_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u68c0\u67e5\u65e5\u5fd7\u6587\u4ef6\/var\/log\/cron\u6216\/var\/log\/syslog\uff0c\u786e\u8ba4\u4efb\u52a1\u662f\u5426\u6210\u529f\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p><strong>\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528\u7cfb\u7edfd\u8ba1\u5212\u4efb\u52a1<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4ec0\u4e48\u662f\u7cfb\u7edfd\u8ba1\u5212\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>systemd\u662f\u4e00\u4e2a\u7cfb\u7edf\u548c\u670d\u52a1\u7ba1\u7406\u5668\uff0c\u53d6\u4ee3\u4e86\u4f20\u7edf\u7684System V init\u7cfb\u7edf\u3002\u5b83\u4f7f\u7528\u5355\u5143\u6587\u4ef6\u63cf\u8ff0\u670d\u52a1\u548c\u8ba1\u5212\u4efb\u52a1\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u5355\u5143\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u6307\u5b9a\u65f6\u95f4\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u914d\u7f6e\u7cfb\u7edfd\u8ba1\u5212\u4efb\u52a1<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u670d\u52a1\u5355\u5143\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo nano \/etc\/systemd\/system\/myscript.service<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[Unit]<\/p>\n<p>Description=My Python Script<\/p>\n<p>[Service]<\/p>\n<p>ExecStart=\/usr\/bin\/python3 \/path\/to\/your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u5b9a\u65f6\u5355\u5143\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo nano \/etc\/systemd\/system\/myscript.timer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[Unit]<\/p>\n<p>Description=Run myscript daily<\/p>\n<p>[Timer]<\/p>\n<p>OnCalendar=daily<\/p>\n<p>Persistent=true<\/p>\n<p>[Install]<\/p>\n<p>WantedBy=timers.target<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u542f\u7528\u548c\u542f\u52a8\u5b9a\u65f6\u5668<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo systemctl enable myscript.timer<\/p>\n<p>sudo systemctl start myscript.timer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u9a8c\u8bc1\u7cfb\u7edfd\u8ba1\u5212\u4efb\u52a1<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u67e5\u770b\u5b9a\u65f6\u5668\u72b6\u6001<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">systemctl status myscript.timer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u67e5\u770b\u65e5\u5fd7<\/strong>\uff1a<\/p>\n<p>systemd\u7684\u65e5\u5fd7\u53ef\u4ee5\u901a\u8fc7journalctl\u547d\u4ee4\u67e5\u770b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">journalctl -u myscript.service<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6587\u4ef6\u6743\u9650<\/strong>\uff1a<\/p>\n<p>\u786e\u4fddPython\u811a\u672c\u6709\u6267\u884c\u6743\u9650\uff0c\u53ef\u4ee5\u901a\u8fc7chmod\u547d\u4ee4\u4fee\u6539\u6587\u4ef6\u6743\u9650\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">chmod +x \/path\/to\/your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u73af\u5883\u53d8\u91cf<\/strong>\uff1a<\/p>\n<p>systemd\u670d\u52a1\u5355\u5143\u6587\u4ef6\u4e2d\u7684ExecStart\u547d\u4ee4\u6267\u884c\u7684\u73af\u5883\u4e0e\u7528\u6237\u7684\u4ea4\u4e92\u5f0fshell\u73af\u5883\u4e0d\u540c\uff0c\u53ef\u80fd\u6ca1\u6709\u52a0\u8f7d\u7528\u6237\u7684\u73af\u5883\u53d8\u91cf\u3002\u56e0\u6b64\uff0c\u5728\u811a\u672c\u4e2d\u9700\u8981\u663e\u5f0f\u8bbe\u7f6e\u5fc5\u8981\u7684\u73af\u5883\u53d8\u91cf\uff0c\u6216\u5728\u670d\u52a1\u5355\u5143\u6587\u4ef6\u4e2d\u6dfb\u52a0Environment\u53d8\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528systemd\u8ba1\u5212\u4efb\u52a1\u5b9a\u65f6\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u5199Python\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672ctest_script.py\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">#!\/usr\/bin\/env python3<\/p>\n<p>import datetime<\/p>\n<p>def main():<\/p>\n<p>    with open(&#39;\/path\/to\/log.txt&#39;, &#39;a&#39;) as f:<\/p>\n<p>        f.write(f&#39;Systemd timer executed at {datetime.datetime.now()}\\n&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u914d\u7f6esystemd\u8ba1\u5212\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u670d\u52a1\u5355\u5143\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo nano \/etc\/systemd\/system\/myscript.service<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[Unit]<\/p>\n<p>Description=My Python Script<\/p>\n<p>[Service]<\/p>\n<p>ExecStart=\/usr\/bin\/python3 \/path\/to\/test_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u5b9a\u65f6\u5355\u5143\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo nano \/etc\/systemd\/system\/myscript.timer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[Unit]<\/p>\n<p>Description=Run myscript daily<\/p>\n<p>[Timer]<\/p>\n<p>OnCalendar=daily<\/p>\n<p>Persistent=true<\/p>\n<p>[Install]<\/p>\n<p>WantedBy=timers.target<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u542f\u7528\u548c\u542f\u52a8\u5b9a\u65f6\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo systemctl enable myscript.timer<\/p>\n<p>sudo systemctl start myscript.timer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u67e5\u770b\u5b9a\u65f6\u5668\u72b6\u6001\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">systemctl status myscript.timer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u67e5\u770b\u65e5\u5fd7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">journalctl -u myscript.service<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u65b9\u6cd5\u4e09\uff1a\u4f7f\u7528\u4e13\u7528\u8c03\u5ea6\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86cron\u548csystemd\uff0cPython\u4e2d\u8fd8\u6709\u4e00\u4e9b\u4e13\u7528\u7684\u8c03\u5ea6\u5e93\uff0c\u5982APScheduler\uff0c\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4ec0\u4e48\u662fAPScheduler<\/h3>\n<\/p>\n<p><p>APScheduler\uff08Advanced Python Scheduler\uff09\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u4f46\u529f\u80fd\u5f3a\u5927\u7684Python\u5e93\uff0c\u7528\u4e8e\u8c03\u5ea6\u4efb\u52a1\u3002\u5b83\u652f\u6301\u57fa\u4e8e\u65f6\u95f4\u3001\u65e5\u671f\u548c\u95f4\u9694\u7684\u4efb\u52a1\u8c03\u5ea6\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728Python\u4ee3\u7801\u4e2d\u5b9e\u73b0\u590d\u6742\u8c03\u5ea6\u903b\u8f91\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b89\u88c5APScheduler<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5APScheduler\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install apscheduler<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528APScheduler<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u5199\u8c03\u5ea6\u811a\u672c<\/strong>\uff1a<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u8c03\u5ea6\u811a\u672cscheduler_script.py\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from apscheduler.schedulers.blocking import BlockingScheduler<\/p>\n<p>import datetime<\/p>\n<p>def job_function():<\/p>\n<p>    with open(&#39;\/path\/to\/log.txt&#39;, &#39;a&#39;) as f:<\/p>\n<p>        f.write(f&#39;APScheduler job executed at {datetime.datetime.now()}\\n&#39;)<\/p>\n<p>scheduler = BlockingScheduler()<\/p>\n<p>scheduler.add_job(job_function, &#39;cron&#39;, hour=3, minute=0)<\/p>\n<p>scheduler.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u8c03\u5ea6\u811a\u672c<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 \/path\/to\/scheduler_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528APScheduler\u5b9a\u65f6\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u5199\u8c03\u5ea6\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u8c03\u5ea6\u811a\u672cscheduler_script.py\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from apscheduler.schedulers.blocking import BlockingScheduler<\/p>\n<p>import datetime<\/p>\n<p>def job_function():<\/p>\n<p>    with open(&#39;\/path\/to\/log.txt&#39;, &#39;a&#39;) as f:<\/p>\n<p>        f.write(f&#39;APScheduler job executed at {datetime.datetime.now()}\\n&#39;)<\/p>\n<p>scheduler = BlockingScheduler()<\/p>\n<p>scheduler.add_job(job_function, &#39;cron&#39;, hour=3, minute=0)<\/p>\n<p>scheduler.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8fd0\u884c\u8c03\u5ea6\u811a\u672c<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 \/path\/to\/scheduler_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4fdd\u6301\u811a\u672c\u8fd0\u884c<\/strong>\uff1a<\/p>\n<p>APScheduler\u662f\u4e00\u4e2aPython\u5e93\uff0c\u9700\u8981\u811a\u672c\u4fdd\u6301\u8fd0\u884c\u72b6\u6001\u624d\u80fd\u6267\u884c\u8c03\u5ea6\u4efb\u52a1\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528\u540e\u53f0\u8fd0\u884c\u5de5\u5177\uff08\u5982screen\u6216tmux\uff09\u6216\u5c06\u811a\u672c\u4f5c\u4e3a\u670d\u52a1\u8fd0\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u73af\u5883\u53d8\u91cf<\/strong>\uff1a<\/p>\n<p>\u786e\u4fddPython\u811a\u672c\u6709\u6267\u884c\u6743\u9650\uff0c\u53ef\u4ee5\u901a\u8fc7chmod\u547d\u4ee4\u4fee\u6539\u6587\u4ef6\u6743\u9650\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">chmod +x \/path\/to\/scheduler_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a<\/p>\n<p>\u53ef\u4ee5\u5728\u8c03\u5ea6\u811a\u672c\u4e2d\u6dfb\u52a0\u65e5\u5fd7\u8bb0\u5f55\uff0c\u4ee5\u4fbf\u8ddf\u8e2a\u4efb\u52a1\u6267\u884c\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u670d\u52a1\u5668\u4e0a\u5b9a\u65f6\u6267\u884cPython\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528cron\u4efb\u52a1\u3001systemd\u8ba1\u5212\u4efb\u52a1\u548c\u4e13\u7528\u8c03\u5ea6\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u53ef\u4ee5\u6839\u636e\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u6765\u51b3\u5b9a\u3002cron\u4efb\u52a1\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5468\u671f\u6027\u4efb\u52a1\uff0csystemd\u8ba1\u5212\u4efb\u52a1\u9002\u7528\u4e8e\u9700\u8981\u66f4\u9ad8\u7ea7\u7ba1\u7406\u7684\u4efb\u52a1\uff0c\u800cAPScheduler\u9002\u7528\u4e8e\u9700\u8981\u5728Python\u4ee3\u7801\u4e2d\u5b9e\u73b0\u590d\u6742\u8c03\u5ea6\u903b\u8f91\u7684\u4efb\u52a1\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6ce8\u610f\u73af\u5883\u53d8\u91cf\u3001\u6587\u4ef6\u6743\u9650\u548c\u65e5\u5fd7\u8bb0\u5f55\u7b49\u95ee\u9898\uff0c\u4ee5\u786e\u4fdd\u4efb\u52a1\u80fd\u591f\u987a\u5229\u6267\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u670d\u52a1\u5668\u4e0a\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\u6765\u8fd0\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728\u670d\u52a1\u5668\u4e0a\u5b9a\u65f6\u6267\u884cPython\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Linux\u7684cron\u4f5c\u4e1a\u3002\u901a\u8fc7\u7f16\u8f91crontab\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u6307\u5b9a\u811a\u672c\u8fd0\u884c\u7684\u65f6\u95f4\u548c\u9891\u7387\u3002\u6253\u5f00\u7ec8\u7aef\uff0c\u8f93\u5165<code>crontab -e<\/code>\u547d\u4ee4\uff0c\u6dfb\u52a0\u4e00\u884c\u7c7b\u4f3c\u4e8e<code>0 * * * * \/usr\/bin\/python3 \/path\/to\/your_script.py<\/code>\uff0c\u8fd9\u5c06\u4f7f\u811a\u672c\u6bcf\u5c0f\u65f6\u8fd0\u884c\u4e00\u6b21\u3002\u786e\u4fdd\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\uff0c\u5e76\u6839\u636e\u60a8\u7684\u9700\u6c42\u8c03\u6574\u65f6\u95f4\u8bbe\u7f6e\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u7684Python\u811a\u672c\u9700\u8981\u8bbf\u95ee\u7279\u5b9a\u7684\u73af\u5883\u53d8\u91cf\uff0c\u5e94\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u5728cron\u4f5c\u4e1a\u4e2d\u8fd0\u884c\u7684Python\u811a\u672c\u53ef\u80fd\u65e0\u6cd5\u8bbf\u95ee\u60a8\u5728\u4ea4\u4e92\u5f0fshell\u4e2d\u8bbe\u7f6e\u7684\u73af\u5883\u53d8\u91cf\u3002\u53ef\u4ee5\u5728crontab\u4e2d\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u6216\u8005\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528<code>os.environ<\/code>\u6765\u83b7\u53d6\u6240\u9700\u7684\u73af\u5883\u53d8\u91cf\u3002\u5982\u679c\u9700\u8981\u5728\u811a\u672c\u4e2d\u4f7f\u7528\u7279\u5b9a\u7684\u73af\u5883\uff0c\u8003\u8651\u5728cron\u547d\u4ee4\u524d\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u6216\u8005\u5728Python\u4ee3\u7801\u4e2d\u76f4\u63a5\u8bbe\u7f6e\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u6211\u7684Python\u811a\u672c\u5728\u5b9a\u65f6\u6267\u884c\u65f6\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff1f<\/strong><br \/>\u4e3a\u786e\u4fddPython\u811a\u672c\u5728\u5b9a\u65f6\u6267\u884c\u65f6\u6b63\u5e38\u5de5\u4f5c\uff0c\u5efa\u8bae\u60a8\u5b9a\u671f\u68c0\u67e5\u65e5\u5fd7\u6587\u4ef6\u3002\u53ef\u4ee5\u5728cron\u4f5c\u4e1a\u4e2d\u6dfb\u52a0\u8f93\u51fa\u91cd\u5b9a\u5411\uff0c\u4f8b\u5982<code>\/usr\/bin\/python3 \/path\/to\/your_script.py &gt;&gt; \/path\/to\/logfile.log 2&gt;&amp;1<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u5c06\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002\u6b64\u5916\uff0c\u6d4b\u8bd5\u811a\u672c\u5728\u547d\u4ee4\u884c\u4e0b\u7684\u8fd0\u884c\u60c5\u51b5\uff0c\u786e\u4fdd\u6ca1\u6709\u6743\u9650\u95ee\u9898\u6216\u4f9d\u8d56\u7f3a\u5931\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u670d\u52a1\u5668\u4e0a\u5b9a\u65f6\u6267\u884cPython\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528cron\u4efb\u52a1\u3001\u7cfb\u7edfd\u8ba1\u5212\u4efb\u52a1\u548c\u4e13\u7528\u8c03\u5ea6\u5e93\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f [&hellip;]","protected":false},"author":3,"featured_media":1116998,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116992"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1116992"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116992\/revisions"}],"predecessor-version":[{"id":1116999,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116992\/revisions\/1116999"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1116998"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1116992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1116992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1116992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}