执行Linux命令并获取PID

2024-04-23

通常我使用:

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

一切正常。
但是当我想获取子进程ID时,它返回空值。

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

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


你会想要使用subprocess module.

# 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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

执行Linux命令并获取PID 的相关文章

随机推荐