在按钮上调用 shell 脚本按下并再次按下后将其杀死

Call a shell script on a button press & kill it after pressed again

我似乎无法弄清楚如何在调用 python 中终止脚本。有什么想法吗?

"Party.py":

#!/usr/bin/python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.IN, pull_up_down=GPIO.PUD_UP)
input = GPIO.input(11)
import time
import os
#initialise a previous input variable
prev_input = 1
party = 0
while True:
  #take a reading
  input = GPIO.input(11)
  #if the last reading was low and this one high, print
  if ((not prev_input) and input):
    if (party ==  0):
        os.system("./home/tim/bin/dalitest")
        party = 1
    elif (party == 1):
        #Kill the script
        party = 0
    time.sleep(2)
  #update previous input
  prev_input = input
  #slight pause to debounce
  time.sleep(0.05)

dalitest(灯光控制脚本):http://pastebin.com/gAijtVBm

这里调用了一个不同的 python 脚本 os.system("./home/tim/bin/dalitest") 如何终止主脚本中的另一个 Python 脚本?

像这样应该可以解决问题:

产卵

#spawn by path + return immediately  and get pid
pid = os.spawnl(os.P_NOWAIT, "./home/tim/bin/dalitest")

杀戮

#kill pid
os.kill(pid, signal.SIGTERM)

#wait for pid to release it back to the system
#this will block until the child is dead
os.waitpid(pid,0)