{"id":174660,"date":"2024-05-08T18:35:46","date_gmt":"2024-05-08T10:35:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/174660.html"},"modified":"2024-05-08T18:35:51","modified_gmt":"2024-05-08T10:35:51","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%81%9a%e7%ae%80%e5%8d%95%e7%9a%84%e5%ae%9a%e6%97%b6%e5%85%b3%e6%9c%ba%e8%bd%af%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/174660.html","title":{"rendered":"\u5982\u4f55\u7528python\u505a\u7b80\u5355\u7684\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27052235\/c0a4cd39-47c4-45bb-b062-55882f24901b.webp\" alt=\"\u5982\u4f55\u7528python\u505a\u7b80\u5355\u7684\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6\" \/><\/p>\n<p><p><strong>\u4f7f\u7528Python\u521b\u5efa\u7b80\u5355\u7684\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6\u4e3b\u8981\u6d89\u53ca\u5230\u4efb\u52a1\u8c03\u5ea6\u3001\u811a\u672c\u7f16\u5199\u548c\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\u7684\u5e94\u7528\u3002<\/strong> \u5173\u952e\u6b65\u9aa4\u5305\u62ec<strong>\u7f16\u5199\u5173\u673a\u811a\u672c\u3001\u8bbe\u5b9a\u6267\u884c\u65f6\u95f4<\/strong>\u3001<strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>schedule<\/code>\u6765\u8c03\u5ea6<\/strong>\u3001<strong>\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u5173\u673a\u547d\u4ee4<\/strong>\u3002\u5728\u8fd9\u4e9b\u5173\u952e\u6b65\u9aa4\u4e2d\uff0c<strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>schedule<\/code>\u6765\u8fdb\u884c\u4efb\u52a1\u8c03\u5ea6<\/strong>\u662f\u7279\u522b\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u76f4\u89c2\u3001\u6613\u7528\u7684\u65b9\u6cd5\u6765\u5b89\u6392Python\u811a\u672c\u6216\u7a0b\u5e8f\u6309\u7167\u7279\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u6216\u65f6\u95f4\u70b9\u6267\u884c\uff0c\u6781\u5927\u5730\u7b80\u5316\u4e86\u5b9a\u65f6\u4efb\u52a1\u7684\u5b9e\u73b0\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3\u5b9a\u65f6\u5173\u673a\u7684\u9700\u6c42<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199Python\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6\u4e4b\u524d\uff0c\u9996\u5148\u8981\u660e\u786e\u7528\u6237\u7684\u9700\u6c42\u3002\u7528\u6237\u53ef\u80fd\u5e0c\u671b\u5728\u7279\u5b9a\u7684\u65f6\u95f4\uff08\u4f8b\u5982\u6bcf\u5929\u665a\u4e0a11\u70b9\uff09\u6216\u5728\u6267\u884c\u4e86\u4e00\u5b9a\u65f6\u95f4\uff08\u5982\u4f7f\u7528\u7535\u8111\u8d85\u8fc72\u5c0f\u65f6\uff09\u540e\u81ea\u52a8\u5173\u95ed\u7535\u8111\u3002\u4e86\u89e3\u8fd9\u4e9b\u9700\u6c42\u6709\u52a9\u4e8e\u8bbe\u8ba1\u5408\u9002\u7684\u811a\u672c\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u57fa\u7840\u4e0a\uff0c\u5f00\u53d1\u8005\u9700\u8981\u719f\u6089Python\u548c\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u5173\u673a\u547d\u4ee4\u3002\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u5173\u673a\u547d\u4ee4\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u4f8b\u5982\uff0c\u5728Windows\u4e2d\uff0c\u5173\u673a\u547d\u4ee4\u4e3a<code>shutdown \/s \/t time<\/code>\uff0c\u800c\u5728Linux\u7cfb\u7edf\u4e2d\u901a\u5e38\u662f<code>shutdown -h now<\/code>\u6216\u4f7f\u7528<code>systemctl poweroff<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u5199\u5173\u673a\u811a\u672c<\/h3>\n<\/p>\n<p><h4>\u521b\u5efaPython\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\uff0c\u5982<code>shutdown_timer.py<\/code>\u3002\u8fd9\u4e2a\u811a\u672c\u5c06\u5305\u542b\u6267\u884c\u5173\u673a\u547d\u4ee4\u7684\u903b\u8f91\u3002\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u6267\u884c\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def shutdown():<\/p>\n<p>    print(&quot;\u7cfb\u7edf\u5c06\u57281\u5206\u949f\u5185\u5173\u673a\u3002&quot;)<\/p>\n<p>    subprocess.run(&quot;shutdown \/s \/t 60&quot;, shell=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8bbe\u7f6e\u5b9a\u65f6\u903b\u8f91<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5728\u811a\u672c\u4e2d\u52a0\u5165\u5b9a\u65f6\u903b\u8f91\uff0c\u51b3\u5b9a\u4f55\u65f6\u6267\u884c\u5173\u673a\u547d\u4ee4\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528<code>schedule<\/code>\u5e93\u6765\u5b89\u6392\u4efb\u52a1\u6309\u6307\u5b9a\u65f6\u95f4\u6267\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import schedule<\/p>\n<p>import time<\/p>\n<p>def job():<\/p>\n<p>    shutdown()<\/p>\n<p>schedule.every().day.at(&quot;23:00&quot;).do(job)<\/p>\n<p>while True:<\/p>\n<p>    schedule.run_pending()<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5e94\u7528\u4efb\u52a1\u8c03\u5ea6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>schedule<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5b89\u6392\u4efb\u52a1\u5728\u7279\u5b9a\u65f6\u95f4\u6267\u884c\u3002\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4efb\u52a1\u88ab\u5b89\u6392\u5728\u6bcf\u5929\u665a\u4e0a11\u70b9\u6267\u884c\u3002<code>schedule<\/code>\u5e93\u7684\u7075\u6d3b\u6027\u5141\u8bb8\u7528\u6237\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u4e0d\u540c\u7684\u65f6\u95f4\u95f4\u9694\uff0c\u5982\u6bcf\u9694\u4e00\u5c0f\u65f6\u3001\u6bcf\u5929\u6216\u6bcf\u5468\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5<code>schedule<\/code>\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>schedule<\/code>\u4e4b\u524d\uff0c\u9700\u8981\u5148\u901a\u8fc7pip\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install schedule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7f16\u5199\u8c03\u5ea6\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>schedule<\/code>\u5e93\u53ef\u4ee5\u7b80\u5355\u51e0\u884c\u4ee3\u7801\u5b9e\u73b0\u590d\u6742\u7684\u65f6\u95f4\u8c03\u5ea6\u903b\u8f91\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u5b9a\u65f6\u4efb\u52a1\u7684\u5b9e\u73b0\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7cfb\u7edf\u517c\u5bb9\u6027\u548c\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u5728\u8bbe\u8ba1\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6\u65f6\uff0c\u5fc5\u987b\u8003\u8651\u5230\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u95f4\u7684\u517c\u5bb9\u6027\u3002\u7531\u4e8e\u5173\u673a\u547d\u4ee4\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u53ef\u80fd\u6709\u6240\u5dee\u5f02\uff0c\u56e0\u6b64\u5f00\u53d1\u8005\u9700\u8981\u7f16\u5199\u9488\u5bf9\u4e0d\u540c\u7cfb\u7edf\u7684\u903b\u8f91\u4ee5\u786e\u4fdd\u8f6f\u4ef6\u80fd\u5728\u5404\u79cd\u73af\u5883\u4e2d\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u6d4b\u8bd5\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u5f7b\u5e95\u6d4b\u8bd5\u662f\u786e\u4fdd\u811a\u672c\u6309\u9884\u671f\u5de5\u4f5c\u7684\u5173\u952e\u3002\u6d4b\u8bd5\u4e0d\u4ec5\u5305\u62ec\u5728\u4e0d\u540c\u65f6\u95f4\u70b9\u89e6\u53d1\u5173\u673a\u6765\u9a8c\u8bc1\u5b9a\u65f6\u903b\u8f91\u7684\u51c6\u786e\u6027\uff0c\u8fd8\u5305\u62ec\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u6d4b\u8bd5\uff0c\u786e\u4fdd\u5173\u673a\u547d\u4ee4\u88ab\u6b63\u786e\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\uff0c\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u52a0\u5165\u903b\u8f91\u6765\u5224\u65ad\u64cd\u4f5c\u7cfb\u7edf\u7c7b\u578b\uff0c\u7136\u540e\u6267\u884c\u76f8\u5e94\u7684\u5173\u673a\u547d\u4ee4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>platform<\/code>\u5e93\u53ef\u4ee5\u68c0\u6d4b\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5e76\u6839\u636e\u68c0\u6d4b\u7ed3\u679c\u9009\u62e9\u6267\u884c\u54ea\u4e2a\u5173\u673a\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p><strong>\u901a\u8fc7Python\u5f00\u53d1\u7b80\u5355\u7684\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6\u662f\u4e00\u4e2a\u65e2\u5b9e\u7528\u53c8\u5bcc\u6709\u6559\u80b2\u610f\u4e49\u7684\u9879\u76ee\u3002<\/strong> \u5b83\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u5b66\u4e60Python\u7f16\u7a0b\u3001\u7406\u89e3\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\u548c\u638c\u63e1\u4efb\u52a1\u8c03\u5ea6\u7b49\u65b9\u9762\u7684\u673a\u4f1a\uff0c\u8fd8\u80fd\u4ea7\u751f\u4e00\u4e2a\u5bf9\u7528\u6237\u6709\u5b9e\u9645\u5e2e\u52a9\u7684\u5de5\u5177\u3002\u867d\u7136\u8fd9\u4e2a\u9879\u76ee\u76f8\u5bf9\u57fa\u7840\uff0c\u4f46\u5b83\u6db5\u76d6\u7684\u6280\u672f\u70b9\u548c\u6982\u5ff5\u4e3a\u5b66\u4e60\u66f4\u590d\u6742\u7684Python\u5e94\u7528\u6253\u4e0b\u4e86\u575a\u5b9e\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6\uff1f<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528Python\u7684<code>subprocess<\/code>\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u5b9a\u65f6\u5173\u673a\u7684\u811a\u672c\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165<code>subprocess<\/code>\u6a21\u5757\uff0c\u5e76\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6765\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u6765\u8bbe\u7f6e\u5b9a\u65f6\u5668\u3002<\/p>\n<pre><code class=\"language-python\">import time\nimport subprocess\n\n# \u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5ef6\u65f6\u65f6\u95f4\uff08\u4ee5\u5206\u949f\u4e3a\u5355\u4f4d\uff09\ndelay = int(input(&quot;\u8bf7\u8f93\u5165\u5ef6\u65f6\u65f6\u95f4\uff08\u5206\u949f\uff09\uff1a&quot;))\n\n# \u5c06\u5ef6\u65f6\u65f6\u95f4\u8f6c\u6362\u4e3a\u79d2\u6570\ndelay_seconds = delay * 60\n\n# \u5ef6\u65f6\u6267\u884c\u5173\u673a\u547d\u4ee4\ntime.sleep(delay_seconds)\nsubprocess.call(&quot;shutdown \/s \/t 0&quot;, shell=True)\n<\/code><\/pre>\n<p><strong>2. \u5982\u4f55\u6d4b\u8bd5\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\uff1f<\/strong><\/p>\n<p>\u4e3a\u4e86\u6d4b\u8bd5\u60a8\u7f16\u5199\u7684\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u5f88\u77ed\u7684\u5ef6\u65f6\u65f6\u95f4\uff0c\u4f8b\u59821\u5206\u949f\uff0c\u7136\u540e\u8fd0\u884c\u811a\u672c\u3002\u7b49\u5f851\u5206\u949f\u540e\uff0c\u60a8\u7684\u8ba1\u7b97\u673a\u5e94\u81ea\u52a8\u5173\u673a\u3002\u5982\u679c\u8ba1\u7b97\u673a\u6210\u529f\u5173\u673a\uff0c\u5219\u8868\u793a\u60a8\u7684\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4fee\u6539\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6\u7684\u65f6\u95f4\uff1f<\/strong><\/p>\n<p>\u8981\u4fee\u6539\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6\u7684\u65f6\u95f4\uff0c\u60a8\u53ea\u9700\u4fee\u6539\u811a\u672c\u4e2d\u7684<code>delay<\/code>\u53d8\u91cf\u7684\u503c\u5373\u53ef\u3002\u6b64\u53d8\u91cf\u8868\u793a\u5ef6\u65f6\u65f6\u95f4\uff08\u4ee5\u5206\u949f\u4e3a\u5355\u4f4d\uff09\u3002\u5c06\u5176\u8bbe\u7f6e\u4e3a\u60a8\u60f3\u8981\u7684\u65b0\u5ef6\u65f6\u65f6\u95f4\uff08\u4ee5\u5206\u949f\u4e3a\u5355\u4f4d\uff09\uff0c\u7136\u540e\u4fdd\u5b58\u66f4\u6539\u5e76\u91cd\u65b0\u8fd0\u884c\u811a\u672c\u5373\u53ef\u3002\u8bf7\u8bb0\u4f4f\uff0c\u60a8\u9700\u8981\u5c06\u5ef6\u65f6\u65f6\u95f4\u8f6c\u6362\u4e3a\u79d2\u6570\uff0c\u56e0\u4e3a\u811a\u672c\u4e2d\u7684\u5ef6\u65f6\u51fd\u6570<code>time.sleep()<\/code>\u63a5\u53d7\u7684\u53c2\u6570\u662f\u4ee5\u79d2\u4e3a\u5355\u4f4d\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u521b\u5efa\u7b80\u5355\u7684\u5b9a\u65f6\u5173\u673a\u8f6f\u4ef6\u4e3b\u8981\u6d89\u53ca\u5230\u4efb\u52a1\u8c03\u5ea6\u3001\u811a\u672c\u7f16\u5199\u548c\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\u7684\u5e94\u7528\u3002 \u5173\u952e\u6b65\u9aa4\u5305\u62ec\u7f16\u5199\u5173\u673a [&hellip;]","protected":false},"author":3,"featured_media":174668,"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\/174660"}],"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=174660"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/174660\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/174668"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=174660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=174660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=174660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}