1

I'm making gpio scripts on my rpi and I want a core program to run and kill another python script.

I had some trouble to find an explanation on how to do this but finally went for this solution : How to terminate a python subprocess launched with shell=True. So my code is :

# // on button pressed, turn on the lcd
spawnProcess = subprocess.Popen("python jukebox_lcd.py", shell=True, preexec_fn=os.setsid)
print "pid : ", os.getpgid(spawnProcess.pid)
# // off button pressed, turn off the lcd
os.killpg(os.getpgid(spawnProcess.pid), signal.SIGTERM)

It will return for example pid : 5030 but will not turn off the lcd... The process I need to kill is 5031 :

    volumio@volumio:~$ ps -elf | grep python
4 S root      4991  2760  0  80   0 -  1307 -      23:11 pts/0    00:00:00 sudo python jukebox-buttons.py
4 S root      4995  4991  3  80   0 -  8352 -      23:11 pts/0    00:00:00 python jukebox-buttons.py
0 S root      5030  4995  0  80   0 -   456 -      23:11 ?        00:00:00 /bin/sh -c python /home/FTP/jukebox_lcd.py
0 S root      5031  5030  1  80   0 -  2062 -      23:11 ?        00:00:00 python /home/FTP/jukebox_lcd.py
0 S volumio   5035  3952  0  80   0 -   648 pipe_w 23:11 pts/1    00:00:00 grep python

I don't understand why my subprocess runs "/bin/sh -c python myscript" and how to fix this. Any help please?

Community
  • 1
  • 1
WorkInProgress
  • 103
  • 1
  • 10
  • 1
    why not just kill spawnProcess.pid? is it the process id you wanted to kill? i think you have /bin/sh because you set shell to True. – Shiping Feb 03 '17 at 00:09
  • os.getpgid(spawnProcess.pid) and spawnProcess.pid give me the same result. Without shell set to True my script is not launch and I want to kill the x+1 process in order to stop my script. Is there a proper way to do it? – WorkInProgress Feb 03 '17 at 00:26
  • how about spawnProcess.kill()? – Shiping Feb 03 '17 at 01:10

1 Answers1

0
spawnProcess = subprocess.Popen(['python', 'jukebox_lcd.py'], preexec_fn=os.setsid)

Everything is working fine nevermind :)

WorkInProgress
  • 103
  • 1
  • 10