warrawind
warrawind

Reputation: 25

How to kill an outside process using python?

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?

Upvotes: 0

Views: 864

Answers (2)

lllrnr101
lllrnr101

Reputation: 2343

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>')

Upvotes: 1

Rohan Asokan
Rohan Asokan

Reputation: 686

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.

Upvotes: 0

Related Questions