Currently tying to stop the middle of a command if the user presses a certain button. I tried this bit of code below and it doesn't seem to work. I know that you can use control-C to have a KeyboardInterrupt, but I would like it so that I could use a button.

import os
def put(command):

elif (RPIO.input(25) == GPIO.LOW):#button 3
        if direct == "":
            while True:
                put("sudo ffmpeg -i \"" + q3 + "\" -f s16le -ar 22.05k -ac 1 - | sudo ./pifm - " + str(freq)) 
                rebootpro() #reboot the program
                if RPIO.input(25) == GPIO.LOW: #if the button is pushed, stop the program.
            put("sudo ffmpeg -i \"" + q3 + "/" + direct + "\" -f s16le -ar 22.05k -ac 1 - | sudo ./pifm - " + str(freq))

I am trying to stop the program while a os.system command is running, is there any way to do this? (python 2.7)

Thanks, Cody

  • Do you need to stop the os.system command too, or just break out of the python? You may need to look into other methods to launch whatever is in os.system() if you just need to break the python code. To break both, i think you will be looking at multiple scripts and "watchdogs" to look for the input separate from the main script.
    – Butters
    Jun 20 '14 at 19:18
  • Just a tip, don't use os.system(), use subprocess.call(). os.system calls your command through the shell, which isn't necessary and can be a security risk. subprocess.call just creates a new process. Read here and here for more explanation. Jun 20 '14 at 22:20
  • Have a look at subprocess.Popen. This will open the program in the background, instead of waiting for the program to finish. That way you can still run python code. Use Popen.poll() to see in the program is still running. Use Popen.kill() to terminate the program. See the documentation
    – Gerben
    Jun 21 '14 at 11:32
  • Thanks for all your help, I will looking into subprocess sense that is what most of you suggest. I will also be removing the os.system command, again thank for the help! Jun 21 '14 at 20:31
  • @Gerben Thanks for the help so much! Could you give an example of how to use the subprocess.Popen and kill it when you want? You'll have to excuse me as I am a beginner, thanks for the help! Jun 21 '14 at 21:09
import shlex, subprocess

while True:
  command_line = "sudo ffmpeg -i \"" + q3 + "\" -f s16le -ar 22.05k -ac 1 - | sudo ./pifm - " + str(freq)
  args = shlex.split(command_line)
  p = subprocess.Popen(args)
  while p.poll() is None and RPIO.input(25)==GPIO.HIGH :

  if RPIO.input(25) == GPIO.LOW :
  //else rerun command again
  • I know this is like a month old, but I thought I would let you know how this went. I get an error when I run this EXACT command. File "./sub2.py", line 8, in <module> args = shlex.split(command_line) File "/usr/lib/python2.7/shlex.py", line 279, in split return list(lex) File "/usr/lib/python2.7/shlex.py", line 269, in next token = self.get_token() File "/usr/lib/python2.7/shlex.py", line 96, in get_token raw = self.read_token() File "/usr/lib/python2.7/shlex.py", line 172, in read_token raise ValueError, "No closing quotation" ValueError: No closing quotation Aug 21 '14 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.