0

I have a python script it sends data permanently and after seeing its output I want to stop or kill the python script .in lettuce I couldn't manage to kill python script.Any idea for this? I tried that:

os.system('pgrep -f sample.py')
    time.sleep(3.0)
 os.system('pkill -9 -f sample.py')
laura
  • 357
  • 3
  • 16

1 Answers1

0

Try subprocess, its much more clean and elegant the command output can be read via the p.communicate() after executing the command.

p = subprocess.Popen(['pgrep', '-f', 'sample.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
pid = p
time.sleep(3.0)
os.kill(pid=pid, sig=9)
Aamir Shaikh
  • 345
  • 1
  • 2
  • 13
  • didnt work :( I stopped with Ctrl+C manually ..after then it tries to read subprocess commands. – laura Apr 16 '18 at 08:45