0

通常我使用:

os.popen("du folder >> 1.txt ").read()

它工作正常。
但是当我想获取子进程 id 时,它返回空值。

os.popen("du folder >> 1.txt &").read() # Notice the & symbol

有谁知道为什么以及如何获得PID?

4

2 回答 2

8

您将需要使用该subprocess模块。

# Can't use shell=True if you want the pid of `du`, not the
# shell, so we have to do the redirection to file ourselves
proc = subprocess.Popen("/usr/bin/du folder", stdout=file("1.txt", "ab"))
print "PID:", proc.pid
print "Return code:", proc.wait()
于 2013-10-03T06:30:29.117 回答
0

&将进程置于后台和作业编号!= pid。获取进程的 pid。

我建议使用subprocess -Popen实例具有pid您可以直接访问的属性。

于 2013-10-03T06:30:56.313 回答