0

I'm running strandtest.py to power a neo pixel matrix on a django server.

A button click runs the program

I'd like a second button click to kill it.

I've read a bunch of different solutions but I am having trouble following the instructions. Is there another subprocess command I can execute while the program runs?

This is how I start the program

import subprocess

def test(request):
    sp = subprocess.Popen('sudo python /home/pi/strandtest.py', shell=True)
    return HttpResponse()

I want to kill the process and clear the matrix.

Yugandhar Chaudhari
  • 3,831
  • 3
  • 24
  • 40
  • 1
    [This question seems related](https://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true) note that if the killing is a response to user intereaction with your HTML, then you will need to store a reference to the created process that can be accessed from the code in another view. You will probably need to store the process id to be able to retrieve the actual process when the code in the view that responds to the 'kill' button executes – ivissani Jun 25 '19 at 13:29

1 Answers1

0

You can try to run below command with subprocess.Popen:

kill -9 `ps aux | grep /home/pi/strandtest.py | awk -F' ' '{print $2}'`

kill -9 - will kill your process

ps aux combine with grep - to find only process you need

awk - get only id of that process

kkarczewski
  • 331
  • 3
  • 19
  • I have not been able to get this to work. subprocess.Popen(kill -9 `ps aux | grep /home/pi/strandtest.py | awk -F' ' '{print $2}'`) Im sure Im missing something here. – Peter Aitchison Jun 25 '19 at 15:47
  • To run it you need admin rights, did you try with sudo? – kkarczewski Jun 26 '19 at 06:33
  • I got it to stop the animation after I realized I needed to use " " around the ' ' in the solution provided. Now I am trying to figure out how to clear the animation. I will get there soon enough. Thanks for the help – Peter Aitchison Jun 27 '19 at 08:33