Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在后台执行命令_Python_Shell - Fatal编程技术网

Python 在后台执行命令

Python 在后台执行命令,python,shell,Python,Shell,尝试使用pythons命令模块在后台执行shell命令 >>>import commands >>>output = commands.getstatusoutput("find / > tmp.txt &") sh: Syntax error: ";" unexpected 有人能解释一下语法的错误吗?那么应该如何执行呢 Tazim.我不知道在这样的后台直接执行命令的方法。特别是,这是因为commands模块不通过bash运行命令,ba

尝试使用pythons命令模块在后台执行shell命令

>>>import commands
>>>output = commands.getstatusoutput("find / > tmp.txt &")
   sh: Syntax error: ";" unexpected
有人能解释一下语法的错误吗?那么应该如何执行呢


Tazim.

我不知道在这样的后台直接执行命令的方法。特别是,这是因为commands模块不通过bash运行命令,bash通常解析符号


您应该从操作系统模块执行fork/exec调用,以便在后台运行。

我不知道在后台直接执行这样的命令的方法。特别是,这是因为commands模块不通过bash运行命令,bash通常解析符号

您应该从操作系统模块执行fork/exec调用,以便在后台运行操作。

根据命令,getstatusoutputcmd作为

{ cmd  ; } 2>&1
因此,您的命令会像以前一样运行

{ find / > tmp.txt & ; } 2 >& 1
以及;在此类命令中,&之后无效

您应该使用来模拟老式的os.spawn命令

试一试

根据命令,commands.getstatusoutputcmd按如下方式执行

{ cmd  ; } 2>&1
因此,您的命令会像以前一样运行

{ find / > tmp.txt & ; } 2 >& 1
以及;在此类命令中,&之后无效

您应该使用来模拟老式的os.spawn命令

试一试


尝试创建用于在后台运行流程的。

尝试创建用于在后台运行流程的。

+1用于提及子流程模块。我会去掉剩下的脆弱的松弛部分,或者至少做个笔记,因为它是严格针对尼克斯的。有几种方法可以使用子流程模块以跨平台的方式处理python中的stdin stdout stderr。提及子流程模块+1。我会去掉剩下的脆弱的松弛部分,或者至少做个笔记,因为它是严格针对尼克斯的。有几种方法可以使用subprocess模块以跨平台的方式处理python中的stdin stdout stderr。