mobile ecommerce development

How to run a long running process on a Raspberry Pi

Using tmux on your Raspberry Pi The tmux package allows you to create a separate shell which you can detach from and let it run in the background. If you had a one-off program that you know is going to take a long time (a make build command for instance), you can create a tmux session, run your long running command, detach from that session, and logout of the pi without worry. As long as you don’t interrupt the pi’s power supply, your script should continue to run. You can attach to the session and check in on it periodically and detach to let it run.

1. Install the tmux package.

sudo apt-get install -y tmux

2. Create a new tmux session.

# pick any session name you like
tmux new -s your_session_name

python3 long_running_script.py

3. Detach from the session by using the following keys:

Hit “Ctrl + B”
Then hit “D”
You’ll be returned to your original session, with your long running script running in the background. You can log out at this point.

4. To re-attach to the session to check how it’s going, simply run the following

# list all the sessions running
tmux list-sessions

# attach to your session
tmux a -t your_session_name
And that’s it. You can detach as described in step 4 again if you like. Tmux is best used for commands that you’re running one-off, like an installation step. If you’re looking to run something long term, continuously, with automatic restarting - you’ll want to create a service.

Get in touch