title: Start and stop application within python using the RPi GPIO?
body: <p>I'm using this python script:</p>
  
  <pre><code>import RPi.GPIO as GPIO
  import subprocess
  from time import sleep
  
  GPIO.setmode(GPIO.BCM)
  GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  GPIO.setup(25, GPIO.OUT, initial=GPIO.LOW)
  
  try:  
      while True:
          if GPIO.input(4):
              GPIO.output(25, 1)
              subprocess.call("/script/start.sh", shell=True)
          else:
              GPIO.output(25, 0)
              subprocess.call("/script/stop.sh", shell=True)
          sleep(0.2)
  
  finally:
      GPIO.cleanup()
  </code></pre>
  
  <p>(Without subprocess) when I press the button (GPIO 4) an LED goes on, press it again and it's off. Works great. I'm currently using 2 scripts to start and stop an application. Those work fine when used outside of Python. When I try to implement the subprocess stuff things start to go wonky.</p>
  
  <p>The scripts are:</p>
  
  <pre><code>#!/bin/bash
  FILENAME=$(date +"%Y%m%d_%H%M")
  rec -c 2 -b 24 -r 48000 -C 320.99 /mnt/usb/${FILENAME}.mp3
  exit
  </code></pre>
  
  <p>and</p>
  
  <pre><code>#!/bin/bash
  reco=`pidof -s rec`
  kill $reco
  exit
  </code></pre>
  
  <p>The process will be started, and it'll keep running, but the python script won't shut it down unless I hit ctrl+c, defeating the point of using the GPIO switch.</p>
  
  <p>What I want to achieve is:</p>
  
  <pre><code>switch = high -&gt; application starts
  switch = low -&gt; application stops
  </code></pre>
  
  <p>Wait/loop so I can start recording again if I press the switch.</p>
  
  <p>How can I get this working properly?</p>