0

I need to write a simple bash script that kills a python script that is running in a terminal window. I'm assuming it will use kill -PID but I don't know how to get the PID of something based on what's running.

I'm sure this is elementary for someone experienced in bash scripts, but I am very new to the topic.

I appreciate all of your time, thanks so much!

Carson P
  • 101
  • 1
  • Seems like the design of the Python script should be fixed instead? – user535733 Jul 03 '18 at 14:41
  • @user535733 the python script is supposed to run continuously until a certain time, I need essentially a kill switch to shut it down no matter what the PID is, so I'm looking to make a bash script to do that – Carson P Jul 03 '18 at 14:43
  • @PerlDuck after reading that question you mentioned, I figured out to have it tell me the PID and alot more, but I'm having trouble getting it to essentially return PID to put it in python terms. – Carson P Jul 03 '18 at 14:44
  • Precisely. If running continuously, then it could be designed as a service, and supervised (started/stopped) by systemd along with all the other services. It's more complex up front to write and test, but easier to maintain across years. – user535733 Jul 03 '18 at 14:44
  • @user535733 can you point me in the right direction as to how to make it a service and how to supervise it? – Carson P Jul 03 '18 at 14:46
  • @PerlDuck never mind, just saw exactly what I need in that link. Thanks for the referral! – Carson P Jul 03 '18 at 14:48

1 Answers1

2

Let's say your script is named test_script.py. You can add an interpreter comment at the start of the script, like so:

#!/usr/bin/env python

def function:
  #example function

#rest of script here

Then, execute chmod: sudo chmod +x test_script.py

And execute it like so: ./test_script.py

Then to kill it, get the id of the script with the command: pidof test_script.py

And it will return the pid, kill it using kill -9 [pid_here]

Tudor
  • 121