a subprocess qns

M

micklee74

hi
i wanted to start execute a command and put it in the background. i am
using Unix.
Usually i start this command using something like this :
"/path/somecmd &" with the "&" to put it to background.
i looked at the subprocess module docs and came across this statement
Replacing os.spawn*
-------------------
P_NOWAIT example:
pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
==>
pid = Popen(["/bin/mycmd", "myarg"]).pid


Can i ask if P_NOWAIT means the same as "&" ?? so if it is, then this
statement
pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")

will put mycmd into background and return to the caller...?

thanks
 
L

Lawrence D'Oliveiro

i wanted to start execute a command and put it in the background. i am
using Unix.
Usually i start this command using something like this :
"/path/somecmd &" with the "&" to put it to background.

Even if it's in the background with "&", it is still liable to get
killed with a SIGHUP signal when you log off.

To stop this, you need to make sure the process is in its own process
session group, by using the setsid system call. Not sure if there's any
standard Python binding for this: but there is a setsid command that you
could use from the shell.
 
B

Ben C

hi
i wanted to start execute a command and put it in the background. i am
using Unix.

If you use subprocess, or even os.spawn, it should be portable and work
on all systems (although the docs list some restrictions).
Usually i start this command using something like this :
"/path/somecmd &" with the "&" to put it to background.

You can still do that: os.system("/bin/mycmd &"), or use
subprocess.Popen with True in its shell parameter. os.system invokes the
shell, so this is not portable-- you must have a shell in which & means
what you want (it works at least on bash and probably on some other Unix
shells).
i looked at the subprocess module docs and came across this statement
Replacing os.spawn*
-------------------
P_NOWAIT example:
pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
==>
pid = Popen(["/bin/mycmd", "myarg"]).pid


Can i ask if P_NOWAIT means the same as "&" ?? so if it is, then this
statement

I think it does.
pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")

will put mycmd into background and return to the caller...?

Should do.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top