{"id":936572,"date":"2024-12-26T19:30:39","date_gmt":"2024-12-26T11:30:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/936572.html"},"modified":"2024-12-26T19:30:41","modified_gmt":"2024-12-26T11:30:41","slug":"python-%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8shell","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/936572.html","title":{"rendered":"python \u5982\u4f55\u4f7f\u7528shell"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072805\/1c226c44-1662-4af3-961b-e36bd79a66ef.webp\" alt=\"python \u5982\u4f55\u4f7f\u7528shell\" \/><\/p>\n<p><p> <strong>Python \u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 subprocess \u6a21\u5757\u3001os \u6a21\u5757\u3001\u6216\u8005\u901a\u8fc7\u96c6\u6210\u5de5\u5177\u5982 Fabric \u6216 Paramiko \u6765\u6267\u884c shell \u547d\u4ee4\u548c\u811a\u672c\u3002subprocess \u6a21\u5757\u88ab\u8ba4\u4e3a\u662f\u6700\u73b0\u4ee3\u548c\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e0e\u5e95\u5c42\u64cd\u4f5c\u7cfb\u7edf\u63a5\u53e3\u7684\u5f3a\u5927\u529f\u80fd\u3002<\/strong>\u5728\u4f7f\u7528 subprocess \u6a21\u5757\u65f6\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7 subprocess.run() \u51fd\u6570\u6765\u8c03\u7528 shell \u547d\u4ee4\uff0c\u8fd9\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u5b89\u5168\u6027\u548c\u9519\u8bef\u5904\u7406\u80fd\u529b\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 subprocess \u6a21\u5757\u6765\u6267\u884c shell \u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001SUBPROCESS \u6a21\u5757<\/p>\n<\/p>\n<p><p>subprocess \u6a21\u5757\u662f Python \u4e2d\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u7684\u63a8\u8350\u65b9\u5f0f\u3002\u5b83\u4e0d\u4ec5\u7075\u6d3b\uff0c\u800c\u4e14\u5b89\u5168\u6027\u9ad8\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>subprocess.run()<\/strong><\/p>\n<\/p>\n<p><p><code>subprocess.run()<\/code> \u662f subprocess \u6a21\u5757\u4e2d\u6700\u5e38\u7528\u7684\u51fd\u6570\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u6267\u884c\u4e00\u4e2a\u547d\u4ee4\uff0c\u5e76\u7b49\u5f85\u547d\u4ee4\u6267\u884c\u5b8c\u6210\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a CompletedProcess \u5b9e\u4f8b\uff0c\u5305\u542b\u6267\u884c\u7ed3\u679c\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>result = subprocess.run([&#39;ls&#39;, &#39;-l&#39;], capture_output=True, text=True)<\/p>\n<p>print(result.stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>subprocess.run()<\/code> \u6765\u6267\u884c <code>ls -l<\/code> \u547d\u4ee4\uff0c\u5e76\u901a\u8fc7 <code>capture_output=True<\/code> \u53c2\u6570\u6355\u83b7\u547d\u4ee4\u7684\u8f93\u51fa\uff0c\u7136\u540e\u901a\u8fc7 <code>result.stdout<\/code> \u6253\u5370\u8f93\u51fa\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>subprocess.Popen()<\/strong><\/p>\n<\/p>\n<p><p><code>subprocess.Popen()<\/code> \u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u66f4\u590d\u6742\u7684\u4efb\u52a1\uff0c\u5982\u7ba1\u9053\u548c\u5e76\u53d1\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>process = subprocess.Popen([&#39;ls&#39;, &#39;-l&#39;], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<p>stdout, stderr = process.communicate()<\/p>\n<p>print(stdout.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 <code>subprocess.Popen()<\/code> \u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u6d41\uff0c\u8fd9\u5728\u9700\u8981\u5904\u7406\u547d\u4ee4\u8f93\u51fa\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6267\u884c shell \u547d\u4ee4\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u9519\u8bef\u3002subprocess \u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u5f02\u5e38\u7684\u673a\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>try:<\/p>\n<p>    result = subprocess.run([&#39;false&#39;], check=True)<\/p>\n<p>except subprocess.CalledProcessError as e:<\/p>\n<p>    print(f&quot;Command f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led with exit code {e.returncode}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>false<\/code> \u662f\u4e00\u4e2a\u8fd4\u56de\u975e\u96f6\u9000\u51fa\u72b6\u6001\u7684\u547d\u4ee4\u3002\u901a\u8fc7 <code>check=True<\/code> \u53c2\u6570\uff0csubprocess \u4f1a\u5728\u547d\u4ee4\u5931\u8d25\u65f6\u629b\u51fa <code>CalledProcessError<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001OS \u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1 subprocess \u6a21\u5757\u662f\u63a8\u8350\u7684\u65b9\u5f0f\uff0cos \u6a21\u5757\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6267\u884c shell \u547d\u4ee4\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>os.system()<\/strong><\/p>\n<\/p>\n<p><p><code>os.system()<\/code> \u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u4e00\u4e2a shell \u547d\u4ee4\uff0c\u4f46\u5b83\u4e0d\u63d0\u4f9b\u6355\u83b7\u8f93\u51fa\u7684\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>os.system(&#39;ls -l&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u7f3a\u70b9\u662f\u65e0\u6cd5\u8bbf\u95ee\u547d\u4ee4\u7684\u8f93\u51fa\uff0c\u5982\u679c\u9700\u8981\u6355\u83b7\u8f93\u51fa\uff0c\u5efa\u8bae\u4f7f\u7528 subprocess \u6a21\u5757\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>os.popen()<\/strong><\/p>\n<\/p>\n<p><p><code>os.popen()<\/code> \u53ef\u4ee5\u6267\u884c\u547d\u4ee4\u5e76\u83b7\u53d6\u8f93\u51fa\uff0c\u4f46\u5df2\u88ab subprocess \u6a21\u5757\u6240\u53d6\u4ee3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>with os.popen(&#39;ls -l&#39;) as stream:<\/p>\n<p>    output = stream.read()<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136 os.popen() \u53ef\u4ee5\u6355\u83b7\u8f93\u51fa\uff0c\u4f46\u5b83\u5df2\u88ab\u6807\u8bb0\u4e3a\u8fc7\u65f6\uff0c\u66f4\u63a8\u8350\u4f7f\u7528 subprocess \u6a21\u5757\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001FABRIC \u548c PARAMIKO<\/p>\n<\/p>\n<p><p>Fabric \u548c Paramiko \u662f\u7528\u4e8e Python \u7684\u9ad8\u7ea7\u5de5\u5177\uff0c\u9002\u5408\u9700\u8981\u901a\u8fc7 SSH \u6267\u884c\u8fdc\u7a0b\u547d\u4ee4\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Fabric<\/strong><\/p>\n<\/p>\n<p><p>Fabric \u662f\u4e00\u4e2a Python \u5e93\u548c\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u7528\u4e8e\u7b80\u5316 SSH \u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fabric import Connection<\/p>\n<p>result = Connection(&#39;hostname&#39;).run(&#39;ls -l&#39;, hide=True)<\/p>\n<p>print(result.stdout.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Fabric \u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 API \u6765\u6267\u884c\u8fdc\u7a0b\u547d\u4ee4\uff0c\u5e76\u81ea\u52a8\u5904\u7406\u8fde\u63a5\u548c\u8eab\u4efd\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Paramiko<\/strong><\/p>\n<\/p>\n<p><p>Paramiko \u662f\u4e00\u4e2a\u7528\u4e8e\u5b9e\u73b0 SSH \u534f\u8bae\u7684\u5e95\u5c42\u5e93\uff0c\u9002\u5408\u9700\u8981\u66f4\u7cbe\u7ec6\u63a7\u5236\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import paramiko<\/p>\n<p>ssh = paramiko.SSHClient()<\/p>\n<p>ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())<\/p>\n<p>ssh.connect(&#39;hostname&#39;, username=&#39;user&#39;, password=&#39;passwd&#39;)<\/p>\n<p>stdin, stdout, stderr = ssh.exec_command(&#39;ls -l&#39;)<\/p>\n<p>print(stdout.read().decode())<\/p>\n<p>ssh.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Paramiko \u63d0\u4f9b\u4e86\u5bf9 SSH \u8fde\u63a5\u7684\u5b8c\u5168\u63a7\u5236\uff0c\u5305\u62ec\u5bc6\u94a5\u9a8c\u8bc1\u548c\u901a\u9053\u7ba1\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001SHELL \u811a\u672c\u7684\u96c6\u6210<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5728 Python \u4e2d\u6267\u884c\u590d\u6742\u7684 shell \u811a\u672c\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7 subprocess \u6a21\u5757\u7684\u7ec4\u5408\u547d\u4ee4\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6267\u884c\u811a\u672c\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u4f7f\u7528 subprocess \u6a21\u5757\u76f4\u63a5\u6267\u884c\u811a\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>subprocess.run([&#39;bash&#39;, &#39;script.sh&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a <code>script.sh<\/code> \u7684 shell \u811a\u672c\u6587\u4ef6\u3002\u901a\u8fc7 subprocess.run() \u53ef\u4ee5\u76f4\u63a5\u6267\u884c\u6b64\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u52a8\u6001\u751f\u6210\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\u751f\u6210\u548c\u6267\u884c\u811a\u672c\u4e5f\u5f88\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>import tempfile<\/p>\n<p>script_content = &quot;&quot;&quot;<\/p>\n<p>echo &quot;This is a dynamically generated script&quot;<\/p>\n<p>ls -l<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>with tempfile.NamedTemporaryFile(&#39;w&#39;, delete=False) as script_file:<\/p>\n<p>    script_file.write(script_content)<\/p>\n<p>    script_file_name = script_file.name<\/p>\n<p>subprocess.run([&#39;bash&#39;, script_file_name])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 tempfile \u6a21\u5757\u521b\u5efa\u4e86\u4e00\u4e2a\u4e34\u65f6\u6587\u4ef6\uff0c\u5199\u5165\u4e86 shell \u811a\u672c\u5185\u5bb9\uff0c\u5e76\u901a\u8fc7 subprocess.run() \u6267\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u7efc\u5408\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u5e94\u7528\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6267\u884c\u591a\u6b65\u547d\u4ee4<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u6267\u884c\u591a\u4e2a\u547d\u4ee4\uff0c\u53ef\u4ee5\u4f7f\u7528 subprocess \u6a21\u5757\u7684\u7ba1\u9053\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>process1 = subprocess.Popen([&#39;ls&#39;, &#39;-l&#39;], stdout=subprocess.PIPE)<\/p>\n<p>process2 = subprocess.Popen([&#39;grep&#39;, &#39;py&#39;], stdin=process1.stdout, stdout=subprocess.PIPE)<\/p>\n<p>process1.stdout.close()<\/p>\n<p>output = process2.communicate()[0]<\/p>\n<p>print(output.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7ba1\u9053\u5c06 <code>ls -l<\/code> \u7684\u8f93\u51fa\u4f20\u9012\u7ed9 <code>grep py<\/code>\uff0c\u8fc7\u6ee4\u51fa\u5305\u542b <code>py<\/code> \u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5927\u91cf\u8f93\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8f93\u51fa\u975e\u5e38\u5927\u7684\u547d\u4ee4\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u8fed\u4ee3\u5668\u9010\u884c\u5904\u7406\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>process = subprocess.Popen([&#39;ls&#39;, &#39;-l&#39;], stdout=subprocess.PIPE, text=True)<\/p>\n<p>for line in iter(process.stdout.readline, &#39;&#39;):<\/p>\n<p>    print(line, end=&#39;&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u5904\u7406\u5927\u91cf\u8f93\u51fa\uff0c\u907f\u514d\u4e00\u6b21\u6027\u8bfb\u53d6\u5bfc\u81f4\u7684\u5185\u5b58\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u4e0e shell \u4ea4\u4e92\u3002subprocess \u6a21\u5757\u662f\u6700\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u5b83\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\u548c\u5b89\u5168\u6027\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002\u5bf9\u4e8e\u9700\u8981\u8fdc\u7a0b\u6267\u884c\u547d\u4ee4\u7684\u60c5\u51b5\uff0cFabric \u548c Paramiko \u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u5728 Python \u4e2d\u9ad8\u6548\u5730\u6267\u884c shell \u547d\u4ee4\u548c\u811a\u672c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884cShell\u547d\u4ee4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>subprocess<\/code>\u6a21\u5757\u6765\u6267\u884cShell\u547d\u4ee4\u3002\u4f7f\u7528<code>subprocess.run()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u547d\u4ee4\u5e76\u83b7\u53d6\u8f93\u51fa\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import subprocess\n\nresult = subprocess.run([&#39;ls&#39;, &#39;-l&#39;], capture_output=True, text=True)\nprint(result.stdout)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>ls -l<\/code>\u547d\u4ee4\u88ab\u6267\u884c\uff0c\u8f93\u51fa\u4f1a\u88ab\u6355\u83b7\u5e76\u6253\u5370\u51fa\u6765\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5728Python\u811a\u672c\u4e2d\u8c03\u7528\u5404\u79cdShell\u547d\u4ee4\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5904\u7406Shell\u547d\u4ee4\u7684\u8f93\u51fa\u548c\u9519\u8bef\u6709\u4f55\u65b9\u6cd5\uff1f<\/strong><br \/>\u901a\u8fc7<code>subprocess.run()<\/code>\u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u8f7b\u677e\u5904\u7406Shell\u547d\u4ee4\u7684\u8f93\u51fa\u548c\u9519\u8bef\u3002<code>returncode<\/code>\u5c5e\u6027\u6307\u793a\u547d\u4ee4\u7684\u6267\u884c\u72b6\u6001\uff0c<code>stdout<\/code>\u548c<code>stderr<\/code>\u5206\u522b\u83b7\u53d6\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">result = subprocess.run([&#39;invalid_command&#39;], capture_output=True, text=True)\n\nif result.returncode != 0:\n    print(&quot;Error:&quot;, result.stderr)\nelse:\n    print(&quot;Output:&quot;, result.stdout)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u547d\u4ee4\u6267\u884c\u5931\u8d25\uff0c\u9519\u8bef\u4fe1\u606f\u5c06\u88ab\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8c03\u7528Shell\u811a\u672c\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8c03\u7528Shell\u811a\u672c\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6765\u907f\u514d\u8def\u5f84\u76f8\u5173\u7684\u95ee\u9898\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6765\u786e\u4fdd\u5b89\u5168\u6027\uff0c\u907f\u514dShell\u6ce8\u5165\u98ce\u9669\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">subprocess.run([&#39;\/path\/to\/script.sh&#39;], check=True)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u6267\u884c\u811a\u672c\u524d\uff0c\u811a\u672c\u5177\u6709\u6267\u884c\u6743\u9650\u3002\u540c\u65f6\uff0c\u4f7f\u7528<code>check=True<\/code>\u53c2\u6570\u53ef\u4ee5\u5728\u547d\u4ee4\u5931\u8d25\u65f6\u629b\u51fa\u5f02\u5e38\uff0c\u65b9\u4fbf\u9519\u8bef\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 subprocess \u6a21\u5757\u3001os \u6a21\u5757\u3001\u6216\u8005\u901a\u8fc7\u96c6\u6210\u5de5\u5177\u5982 Fabric \u6216 Pa [&hellip;]","protected":false},"author":3,"featured_media":936575,"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\/936572"}],"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=936572"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/936572\/revisions"}],"predecessor-version":[{"id":936576,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/936572\/revisions\/936576"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/936575"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=936572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=936572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=936572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}