Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Linux Command and Get PID

Tags:

python

linux

Normally I use:

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

It was working fine.
But when I wanted to grab the subprocess id, it returns the empty value.

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

Does anyone know why and how to get the PID?

like image 485
Ben Avatar asked May 04 '26 02:05

Ben


1 Answers

You'll want to use the 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()
like image 149
AKX Avatar answered May 05 '26 16:05

AKX