get pid of a nohup command in Python

G

g.willgoose

I'm new to Python and am evaluating moving a project from Tcl/Tk to
Python and am stuck on one issue. How can I nohup (or any detached
task for that matter) a process and get its PID in Python. The obvious
route of (with a trivial example)

os.system("nohup ls > junk &")

returns the status of the command execution (ie. 0). I wish to get the
PID output of the command (e.g. 16617). All the other commands spawn**
also seem to return the execution status not the PID. This is a
showstopper so any help appreciated.
 
G

Gabriel Genellina

I'm new to Python and am evaluating moving a project from Tcl/Tk to
Python and am stuck on one issue. How can I nohup (or any detached
task for that matter) a process and get its PID in Python. The obvious
route of (with a trivial example)

os.system("nohup ls > junk &")

returns the status of the command execution (ie. 0). I wish to get the
PID output of the command (e.g. 16617). All the other commands spawn**
also seem to return the execution status not the PID. This is a
showstopper so any help appreciated.

The spawn* family will return the pid if you use the P_NOWAIT flag.
But the best way is to use the subprocess module; Popen objects have a pid
attribute.

http://docs.python.org/lib/module-subprocess.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top