开发者

exeuting command in background

开发者 https://www.devze.com 2023-01-03 10:43 出处:网络
Trying to execute shell command in background using pythons commands modu开发者_JAVA技巧le >>>import commands

Trying to execute shell command in background using pythons commands modu开发者_JAVA技巧le

>>>import commands
>>>output = commands.getstatusoutput("find / > tmp.txt &")
   sh: Syntax error: ";" unexpected

Can anyone explain what is wrong with the syntax ? How should it be executed then ?

Tazim.


According to the getstatusoutput documentation, commands.getstatusoutput(cmd) is executed as

{ cmd  ; } 2>&1

so your command is run as if it was

{ find / > tmp.txt & ; } 2 >& 1

and the ; is not valid after the & in such a command.

You should use the subprocess module to simulate the old-style os.spawn commands.

Try

subprocess.Popen("find / > tmp.txt", shell=True)


I'm not aware of a way to directly execute a command in background like that. Particularly, it's because commands module does not run the command through bash, which is what usually parses the ampersand.

You should do a fork/exec call from the os module to run things in background.


Try to create a daemon to run your process in background.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号