I'm running the following python script to play playlists in my Music library on my ras-pi.

import subprocess
import random
import sys
from os import listdir

files = []
for f in listdir("/home/pi/Music/" + sys.argv[1]):
while len(files) > 0:
    x = random.randint(0, len(files) - 1)
    print x
    var = files[x]
    subprocess.call("omxplayer " + "/home/pi/Music/" + sys.argv[1] + "/" + var, shell=True)

I call the script using python shuffle.py <<<playlist>>>.

I want to be able to run this command and still have access to the shell. Currently the shell prompt vanishes until every song has played.

I've tried adding an & to the subprocess call in the python script but that causes every song to play at once. I've also tried placing the & at the end of the command when I call it in the shell but this results in no music being played.

  • 1
    I am assuming you are using ssh or running your Pi without a GUI. If so you can try screen or tmux both will let you have multiple terminals open. Both can also be installed with apt-get install and a google search will turn up a tutorial. Commented Dec 9, 2015 at 4:09
  • Instead of adding & to subprocess - add the ampersand to the initial python call ("python shuffle.py <<playlist>> &") - that will background the python process, but presumably the subprocess calls will continue to run synchronously?
    – T3am5hark
    Commented Dec 10, 2015 at 3:17

2 Answers 2


You can use screen It allows you to run commands in virtual terminal and then deattach from it and use terminal normally.

To install it use these commands:

sudo apt-get update
sudo apt-get install screen

and then to start new session use

screen -S qwerty

you can use any command there and then press ctrl-a and d to deattach from session.

If you want to get back to screen use this:

screen -r qwerty

The simplest way to do this is to background the python call, i.e:

python shuffle.py <<<playlist>>> &

This will execute the python script in the background, but the subprocess calls will continue to execute synchronously.

Putting the ampersand on the subprocess calls will not block on execution which is why they are all running simultaneously.

  • And you don't have to install anything.
    – Aloha
    Commented Dec 10, 2015 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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