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.
sudo apt-get install -y tmux
# pick any session name you like
tmux new -s your_session_name
python3 long_running_script.py
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.
# 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.