Five Ways to Run Python Scripts in the Background on Linux

This article provides a comprehensive guide to five methods for running Python scripts in the background on Linux, covering nohup with &, screen sessions, systemd services, Celery task queues, and supervisord, including command examples and configuration steps.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Five Ways to Run Python Scripts in the Background on Linux

1. Use nohup command

The nohup command runs a program on a remote server and redirects output to nohup.out. Execute a Python script in the background with: nohup python script.py & The trailing & puts the process in the background and returns a PID; output can be inspected in nohup.out.

2. Use screen command

screen

creates detachable terminal sessions that persist after SSH disconnects. Start a session and run the script:

screen -S mysession
python script.py

Detach with Ctrl+A D to return to the console while the script continues.

3. Use systemd service

Systemd manages services on Linux. Create a unit file /etc/systemd/system/myscript.service:

[Unit]
Description=My Python Script
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python3 /path/to/script.py

[Install]
WantedBy=multi-user.target

Reload the daemon, start the service, and check its status:

sudo systemctl daemon-reload
sudo systemctl start myscript
sudo systemctl status myscript

4. Use Celery task queue

Celery enables asynchronous task execution. Install it, define a task, start a worker, and call the task:

pip install celery
from celery import Celery
app = Celery('task', backend='rpc://', broker='amqp://localhost')

@app.task
def mytask():
    # task logic
celery -A tasks worker
from tasks import mytask
mytask.delay()

5. Use supervisord

Supervisord controls processes. Install it, create a config file /etc/supervisor/conf.d/myscript.conf:

[program:myscript]
command=/usr/bin/python /path/to/script.py
autostart=true
autorestart=true
stderr_logfile=/var/log/myscript.err.log
stdout_logfile=/var/log/myscript.out.log
user=username

Reload the configuration and start the script:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start myscript

These five approaches—nohup, screen, systemd, Celery, and supervisord—provide flexible options for running Python programs as background services on Linux systems.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonlinuxcelerysystemdbackground executionscreennohupSupervisord
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.