1

I'm a new bie to python. Recently, I face a problem with Python Popen, and hope someone can help me. Thanks :D

a.py

#!/usr/bin/env python
import b

b.run()
while True:
     pass

b.py

#!/usr/bin/env python                                                                                                                                                                      
import subprocess

def run():
    subprocess.Popen(['ping www.google.com > /dev/null &'], shell=True)

run()

When run b.py and grep process status

$ ps aux | grep 
test 35806 0.0  0.0  2451284 592 s010  Sl  10:11 0:00.00 ping www.google.com

the ping process runs in background with STATE Sl.

And now I try run a.py

$ ps aux | grep 
test 36088 0.0  0.0  2444116 604 s010  Sl+ 10:15 0:00.00 ping www.google.com

ping process STATE changes to Sl+, and if I stop a.py with ctrl + c, ping process also terminated.

Is there any way to make ping process to run in backgroud, and it will not be affected when I stop a.py? And why ping process STATE changes from Sl to Sl+?

Ta Ching Chen
  • 126
  • 1
  • 7
  • Do you want to create a [Unix daemon process](https://pypi.python.org/pypi/python-daemon/) or is it enough if [`ping` continues to run after `a.py` exits](http://stackoverflow.com/q/13243807/4279)? – jfs Apr 06 '14 at 18:13
  • @J.F.Sebastian: thanks for your suggestion. :D I found add the parameter "preexec_fn=os.setsid" can solve the problem. – Ta Ching Chen Apr 10 '14 at 11:49

1 Answers1

1

After research, I found that we can add "preexec_fn=os.setsid", and it solve the problem.

subprocess.Popen(['ping www.google.com > /dev/null &'], shell=True, preexec_fn=os.setsid)
Ta Ching Chen
  • 126
  • 1
  • 7