I am trying to build a one-button Darkice streaming box, I'm using a Pi 3 B+ with Stretch from NOOBS. I have everything set up but my Python script hangs up as soon as the button is pressed and Darkice runs, using process.call(Darkice). I think this is because it runs Darkice in the foreground, as when one types 'Darkice' in terminal that window becomes 'frozen' until ctrl-c is pressed. How do I have the 'Darkice' command run in my Python script so that Darkice runs in the background and the Python script continues beyond starting Darkice? Apologies if this is super-basic, I'm not a coder.
Your script hangs because it does not resume after it run the
As you described, when running
darkice in the terminal it is starts and runs indefinitely until the user or another process terminates it - in your case the
ctrl-c key combination.
That's why your terminal window appear frozen.
A simple approach with standard CLI tools to run a program in the background could be the unix terminal program
screen, it creates a virtual TTY and comes with raspbian by default.
In your python script you could use the
os library to call screen using the
os.system() function instead of
Place the following line of instead of your
subprocress.call("darkice") in your script:
os.system("screen -S backgroundtty -D -m darkice")
and your script should resume after the command while
darkice will run in the background.
You can stop
darkice using the
kill command or kill the
screen that is running
os.system("screen -X -S backgroundtty kill")