0

I am creating a python programme which will first run another python file and then every 5 mins it will terminate it and restart it untill I manually stop it

I used call from subprocess to start the file every 5 mins but how can I terminate it?

  • Does this answer your question? [How to terminate a python subprocess launched with shell=True](https://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true) – Tomerikoo Jan 07 '21 at 07:29

2 Answers2

1

Python's in-built os.system() function can be used to run any command line.

You can use command kill -9 <program_name> with os.system() as below to kill the other program. You may refer to the following documentation for more clarity.

os.system('killall -9 <program_name>')

or

os.system('pkill <program_name>')

or

os.system('kill -9 <PID>')
lllrnr101
  • 2,288
  • 2
  • 4
  • 15
  • What if I have multiple programs with the same name? It will kill all of them. See my answer for the implementation where you do not want to kill all processes with the same name. – Rohan Asokan Jan 07 '21 at 07:31
0

The call is in the legacy API of subprocess. Instead use the Popen constructor under subprocess - docs here. Once you do this, you can easily send a signal to kill the process after some time as,

p = Popen(args)
p.kill()

p.kill() is OS independent. Hope this helps. Please do comment if you didn't understand.

Rohan Asokan
  • 634
  • 4
  • 22