Run Python Script in Background
Option 1: nuhup
nuhupnuhup python3 {PYTHON_SCRIPT_PATH} &nuhup ./run.py > log.txt &Option 2: tmux
tmuxtmuxpython3 {SCRIPT}Option 3: screen
screenLast updated
A Python script, say a Telegram bot, for example, can be run in many different ways. While using Tmux, Screen do seem easy, what truly stand out was this command nuhup.
nuhupnuhup can be used to run Python script in background by:
nuhup python3 {PYTHON_SCRIPT_PATH} &Note: & is important, kindly add it so that the script can run in background.
By default nuhup writes to nuhup writes to nohup.out which can be changed by redirecting stdout to any file, example:
nuhup ./run.py > log.txt &tmuxUsing tmux isn't that very hard, and can be done with just running tmux and detaching the session.
tmuxThen,
python3 {SCRIPT}Press, ctrl + b and d after it to detach the current shell session.
screenUsing screen isn't that different from using tmux, and can be done by:
Then,
Press, ctrl + a and d immediately after it to detach the current screen shell session.
Source: Janakiev
Last updated
screenpython3 {SCRIPT}