0% found this document useful (0 votes)
13 views5 pages

52.1 Nohup to Run Python in Background on Linux

This document provides a guide on using 'nohup' to run Python scripts in the background on Linux, allowing scripts to continue running after the terminal is closed. It includes step-by-step instructions, example code, and commands for checking and killing background processes. Additionally, it highlights when to use 'nohup' and when alternatives like 'tmux' or 'screen' may be more appropriate.

Uploaded by

joanantoranjith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

52.1 Nohup to Run Python in Background on Linux

This document provides a guide on using 'nohup' to run Python scripts in the background on Linux, allowing scripts to continue running after the terminal is closed. It includes step-by-step instructions, example code, and commands for checking and killing background processes. Additionally, it highlights when to use 'nohup' and when alternatives like 'tmux' or 'screen' may be more appropriate.

Uploaded by

joanantoranjith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Gowtham SB

www.linkedin.com/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: script.py
# script.py
import time
from datetime import datetime

while True:
with open("nohup_log.txt", "a") as f:
Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil
f.write(f"Running at: {datetime.now()}\n")
print(f"Running at: {datetime.now()}")
time.sleep(60) # runs every 1 minute

🔁 Step 1: Run in Background with nohup


nohup python3 script.py &

✅ Explanation:

● nohup → Run without hang-up

● python3 script.py → Your command

● & → Send it to background

📝 This creates a file:


nohup.out

All stdout and stderr (print statements, errors) go here.

🔍 Step 2: Check If It’s Running


ps aux | grep script.py

You’ll see something like:

ubuntu 12345 0.0 0.1 ... python3 script.py

That number (12345) is the PID (process ID).

🛑 Step 3: Kill the Background Script


kill 12345

Or kill all Python scripts with:


Gowtham SB
www.linkedin.com/in/sbgowtham/ Instagram - @dataengineeringtamil
pkill -f script.py

✅ It’s clean and shuts it down instantly.

🧹 Optional: Redirect Output to Custom File

Instead of using default nohup.out, do this:

nohup python3 script.py > log.txt 2>&1 &

● > log.txt → standard output (print)

● 2>&1 → redirect errors to same file

● & → run in background

✅ Summary Table
Command Purpose

nohup python3 Run script in background


script.py &

`ps aux grep script.py`

kill <pid> Kill the background process

pkill -f script.py Kill by script name

tail -f nohup.out 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
www.linkedin.com/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
www.linkedin.com/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.

𝐒𝐨𝐜𝐢𝐚𝐥𝐬

𝐘𝐨𝐮𝐓𝐮𝐛𝐞 - https://www.youtube.com/@dataengineeringvideos

𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - https://instagram.com/dataengineeringtamil

𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - https://instagram.com/thedatatech.in

𝐂𝐨𝐧𝐧𝐞𝐜𝐭 𝐟𝐨𝐫 𝟏:𝟏 - https://topmate.io/dataengineering/

𝐋𝐢𝐧𝐤𝐞𝐝𝐈𝐧 - https://www.linkedin.com/in/sbgowtham/

𝐖𝐞𝐛𝐬𝐢𝐭𝐞 - https://codewithgowtham.blogspot.com

𝐆𝐢𝐭𝐇𝐮𝐛 - http://github.com/Gowthamdataengineer

𝐖𝐡𝐚𝐭𝐬 𝐀𝐩𝐩 - https://lnkd.in/g5JrHw8q

𝐄𝐦𝐚𝐢𝐥 - atozknowledge.com@gmail.com

𝐀𝐥𝐥 𝐌𝐲 𝐒𝐨𝐜𝐢𝐚𝐥𝐬 - https://lnkd.in/gf8k3aCH

You might also like