Gowtham SB
[Link]/in/sbgowtham/ Instagram - @dataengineeringtamil
🚀 Guide: Using nohup to Run Python in Background on
Linux
💡 What is nohup?
● nohup stands for "No Hang Up"
● It lets you run a command or script that keeps running even after you close the
terminal
● It's often used for long-running Python scripts, web scrapers, ETL jobs, etc.
🤔 When to Use nohup
✅ Use nohup when:
Situation Why use nohup?
Running a long-running script Keeps running even if terminal closes
Running on a remote server (via Prevents script from stopping if SSH
SSH) disconnects
Running cron-like loops (while Keeps script alive in background
True)
Logging output to a file Helps debug or monitor
🧪 Step-by-Step: Run Python in Background with nohup
📄 Example: [Link]
# [Link]
import time
from datetime import datetime
while True:
with open("nohup_log.txt", "a") as f:
Gowtham SB
[Link]/in/sbgowtham/ Instagram - @dataengineeringtamil
[Link](f"Running at: {[Link]()}\n")
print(f"Running at: {[Link]()}")
[Link](60) # runs every 1 minute
🔁 Step 1: Run in Background with nohup
nohup python3 [Link] &
✅ Explanation:
● nohup → Run without hang-up
● python3 [Link] → Your command
● & → Send it to background
📝 This creates a file:
[Link]
All stdout and stderr (print statements, errors) go here.
🔍 Step 2: Check If It’s Running
ps aux | grep [Link]
You’ll see something like:
ubuntu 12345 0.0 0.1 ... python3 [Link]
That number (12345) is the PID (process ID).
🛑 Step 3: Kill the Background Script
kill 12345
Or kill all Python scripts with:
Gowtham SB
[Link]/in/sbgowtham/ Instagram - @dataengineeringtamil
pkill -f [Link]
✅ It’s clean and shuts it down instantly.
🧹 Optional: Redirect Output to Custom File
Instead of using default [Link], do this:
nohup python3 [Link] > [Link] 2>&1 &
● > [Link] → standard output (print)
● 2>&1 → redirect errors to same file
● & → run in background
✅ Summary Table
Command Purpose
nohup python3 Run script in background
[Link] &
`ps aux grep [Link]`
kill <pid> Kill the background process
pkill -f [Link] Kill by script name
tail -f [Link] Live view of log output
🔚 When Not to Use nohup
● When you need full job control, use tmux or screen instead.
● When using cron, nohup is not required (cron handles it).
Gowtham SB
[Link]/in/sbgowtham/ Instagram - @dataengineeringtamil
About the Author
Gowtham SB is a Data Engineering expert, educator, and content creator with a
passion for big data technologies, as well as cloud and Gen AI . With years of
experience in the field, he has worked extensively with cloud platforms, distributed
systems, and data pipelines, helping professionals and aspiring engineers master the
art of data engineering.
Gowtham SB
[Link]/in/sbgowtham/ Instagram - @dataengineeringtamil
Beyond his technical expertise, Gowtham is a renowned mentor and speaker, sharing
his insights through engaging content on YouTube and LinkedIn. He has built one of
the largest Tamil Data Engineering communities, guiding thousands of learners to
excel in their careers.
Through his deep industry knowledge and hands-on approach, Gowtham continues to
bridge the gap between learning and real-world implementation, empowering
individuals to build scalable, high-performance data solutions.
𝐒𝐨𝐜𝐢𝐚𝐥𝐬
𝐘𝐨𝐮𝐓𝐮𝐛𝐞 - [Link]
𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - [Link]
𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - [Link]
𝐂𝐨𝐧𝐧𝐞𝐜𝐭 𝐟𝐨𝐫 𝟏:𝟏 - [Link]
𝐋𝐢𝐧𝐤𝐞𝐝𝐈𝐧 - [Link]
𝐖𝐞𝐛𝐬𝐢𝐭𝐞 - [Link]
𝐆𝐢𝐭𝐇𝐮𝐛 - [Link]
𝐖𝐡𝐚𝐭𝐬 𝐀𝐩𝐩 - [Link]
𝐄𝐦𝐚𝐢𝐥 - [Link]@[Link]
𝐀𝐥𝐥 𝐌𝐲 𝐒𝐨𝐜𝐢𝐚𝐥𝐬 - [Link]