0
#!/usr/bin/python
import subprocess
import signal
import time
p=subprocess("adb devices")
p_id=p.pid
time.sleep
os.kill(p_pid,signal.SIGINT)
print "bye"

This is working fine and printing "bye", but the below code is not terminating, keeps on printing log content, and won't print "bye".

#!/usr/bin/python
import subprocess
import signal
import time
p=subprocess("adb logcat -v time event")
p_id=p.pid
time.sleep
os.kill(p_pid,signal.SIGINT)
print "bye"

kindly suggest

Thanks in advance Bushra

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    Any particular reason you're using `os.kill()`, vs `p.kill()`? (The latter will correctly switch over to using `terminate()` when on Windows... on which point, could you specify your OS?) – Charles Duffy May 23 '17 at 19:08
  • BTW, `time.sleep` doesn't do anything at all. Maybe you mean `time.sleep(1)` or such? – Charles Duffy May 23 '17 at 19:10
  • BTW, which version of Python is this? I'd strongly suggest using the `timeout=` argument to `Popen()` if targeting 3.3 or later; that way you get the effect of `sleep()`, but automatically return early if the command exits before the timeout. – Charles Duffy May 23 '17 at 19:18
  • (You may also find https://stackoverflow.com/questions/1191374/using-module-subprocess-with-timeout to be interesting). – Charles Duffy May 23 '17 at 19:20

1 Answers1

0
#!/usr/bin/python
import subprocess
import time
p=subprocess.Popen(['adb', 'logcat', '-v', 'time', 'event'])
time.sleep(1) # wait for a second
p.kill()
print "bye"

Notable items:

  • If you use a shell, your signal is delivered to the shell, not the process it started. To ensure that the signal is delivered direct to the process, run the process directly.
  • time.sleep does absolutely nothing on its own; it needs to be called as a function, as in time.sleep(1), to have any effect.
  • Use p.kill() to let the subprocess module select the appropriate kill mechanism for the current platform, as opposed to os.kill().
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441