Of course! The term "nohup print" in Python usually means you want to run a Python script in the background, so it doesn't stop when you close your terminal (SSH session), and you want to see its output (the print statements) either in real-time or saved to a file.

Let's break this down into two main scenarios:
- The Easy & Modern Way (Recommended): Using a terminal multiplexer like
tmuxorscreen. This gives you a persistent, interactive session. - The Classic Unix Way: Using the
nohupcommand. This is simpler for a one-off command but less interactive.
Scenario 1: The Easy & Modern Way (tmux or screen)
This is the best approach for long-running scripts where you might want to check on their progress, stop them, or see live output.
Using tmux (Terminal Multiplexer)
tmux is a powerful tool that lets you create multiple persistent terminal sessions inside one terminal window.
Step 1: Create a new tmux session
Open your terminal and run:

tmux new -s my_python_session
This creates a new session named my_python_session and you'll be inside it.
Step 2: Run your Python script
Now, inside this tmux session, simply run your Python script as you normally would. The print statements will appear on your screen.
python your_script.py
Step 3: Detach from the session
To leave the session running in the background, press the tmux detach key combination: Ctrl-b then d.
Your script is now running in the background, and you can safely close your terminal or SSH connection.
Step 4: Re-attach to the session later To see the output and re-attach to your running session, use:
tmux attach -t my_python_session
You will see the terminal exactly as you left it, including any new print output that has occurred.
Bonus: Splitting the screen
Inside a tmux session, you can split the window to see your output and run other commands.
- Split vertically:
Ctrl-bthen - Split horizontally:
Ctrl-bthen - Navigate between panes:
Ctrl-bthen an arrow key.
Using screen
screen is the older, more classic version of tmux with similar functionality.
Step 1: Create a new screen session
screen -S my_python_session
Step 2: Run your Python script
python your_script.py
Step 3: Detach from the session
Press the screen detach key combination: Ctrl-a then d.
Step 4: Re-attach to the session later
screen -r my_python_session
Scenario 2: The Classic Unix Way (nohup)
nohup (No Hang Up) is a command that makes a process immune to hangups (i.e., when you log out or close the terminal). By default, it redirects output to a file.
The Basic Command
You can run your Python script directly with nohup.
nohup python your_script.py
What happens here?
nohup: The script will continue running after you log out.python your_script.py: Your script is executed.- Output Redirection: By default,
nohupredirectsstdout(whereprintgoes) to a file namednohup.outin the current directory. - Backgrounding: The command runs in the background. You'll get your terminal prompt back immediately.
The Modern nohup Command (Recommended)
To have more control, it's best to explicitly redirect both output and errors to a specific file.
nohup python your_script.py > my_output.log 2>&1 &
Let's break this down:
nohup: Protects the process from being terminated.python your_script.py: The command to run.> my_output.log: Redirects standard output (stdout) to a file namedmy_output.log.2>&1: Redirects standard error (stderr, where errors and exceptions go) to the same place as standard output (stdout). This is crucial for capturing all your script's output.&: Runs the entire command in the background.
How to check on your script
-
Check the output file: You can
tailthe log file to see the latestprintstatements in real-time.tail -f my_output.log
Press
Ctrl-cto stoptail. -
Find the Process ID (PID): To manage the process (e.g., stop it), you need its Process ID (PID).
ps aux | grep python
Look for the line with your script. The second column is the PID.
-
Stop the process: Once you have the PID, you can kill it.
kill <PID>
For example:
kill 12345
Summary Table
| Method | Best For | How to Run | How to Check Output | How to Stop |
|---|---|---|---|---|
tmux / screen |
Recommended. Interactive scripts, long-term development, server management. | tmux new -s namepython script.pyCtrl-b d |
tmux attach -t name |
Ctrl-c (while attached)kill <PID> |
nohup |
Simple. One-off commands, non-interactive scripts, cron jobs. | nohup python script.py > log.out 2>&1 & |
tail -f log.out |
ps aux \| grep pythonkill <PID> |
Example Python Script
Let's use this simple script for testing. Save it as long_running_task.py.
# long_running_task.py
import time
import datetime
print(f"Script started at: {datetime.datetime.now()}")
for i in range(10):
print(f"Processing item {i+1}...")
time.sleep(2) # Wait for 2 seconds
print(f"Script finished at: {datetime.datetime.now()}")
Now you can test any of the methods above with this script. For example, with nohup:
# Run the script in the background nohup python long_running_task.py > task.log 2>&1 & # Check the status of the process ps aux | grep long_running_task # Watch the output in real-time tail -f task.log
You will see the print statements appear in your terminal every 2 seconds, even if you open a new SSH session.
