Software - General
1827421 Members
4188 Online
109965 Solutions
New Discussion

How I Turned My Python Script into a Linux Service (And Why You Might Want To)

 
Amit_Pisal
HPE Pro

How I Turned My Python Script into a Linux Service (And Why You Might Want To)

Why I Did This (A Short Anecdote)

A few months ago, I built a Python script that scraped logs from a network appliance and saved them to a file every few minutes. It worked fine — until it didn’t.

One weekend, the script silently failed. No email, no logs, no trace. Just… gone. I had to SSH into the server, manually run it, and tail logs. Again.

That's when I realized: if Linux services can manage daemons like sshd, why can’t my Python script enjoy the same VIP treatment?

This blog shows how I turned a plain Python script into a proper systemd service — with logging, restarts, and dependency management. And how you can too.


What You'll Learn

  • How to turn any Python script into a Linux service
  • How to use virtual environments in services
  • How to handle logging, auto-restarts, and monitoring

Step 1: Organize Your Script

Here's a simple Python script that logs timestamps every 60 seconds:

# /opt/python-services/timestamp_logger.py

import time
from datetime import datetime

log_file = "/var/log/timestamp.log"

while True:
    with open(log_file, "a") as f:
        f.write(f"{datetime.now()}\n")

    time.sleep(60)

Make it executable:

chmod +x /opt/python-services/timestamp_logger.py

 

Directory tip: I keep my service code in /opt/python-services/.

Step 2: Create a Virtual Environment (Optional but Recommended)

Use venv to isolate dependencies:

cd /opt/python-services
python3 -m venv venv
source venv/bin/activate
pip install requests  # or any packages your script uses

Step 3: Create a systemd Unit File

Create the service descriptor:

sudo nano /etc/systemd/system/timestamp.service

[Unit]
Description=Python Timestamp Logger Service
After=network.target

[Service]
WorkingDirectory=/opt/python-services
ExecStart=/opt/python-services/venv/bin/python /opt/python-services/timestamp_logger.py
Restart=on-failure
RestartSec=5
User=nobody
Group=nogroup
StandardOutput=journal
StandardError=journal
Environment="PATH=/opt/python-services/venv/bin"

[Install]
WantedBy=multi-user.target

 

️ Security tip: Replace nobody with a dedicated low-privilege user like pyservice.

Step 4: Start, Enable, and Monitor the Service

sudo systemctl daemon-reload
sudo systemctl start timestamp.service
sudo systemctl enable timestamp.service

Check if it’s alive:

sudo systemctl status timestamp.service

Step 5: Logging and Debugging

Your script's print() and errors will show up here:

sudo journalctl -u timestamp.service -f

Or you can configure a log file inside your script:

import logging

logging.basicConfig(filename="/var/log/timestamp.log", level=logging.INFO)
logging.info("Hello from systemd!")

Use logging.exception() inside try...except blocks to capture stack traces.


Auto-Restart & Throttling

These service options make your script more resilient:

Restart=on-failure
RestartSec=5
StartLimitBurst=3
StartLimitIntervalSec=30

This ensures:

  • The script restarts on failure
  • It won’t enter a restart loop if crashing repeatedly

Troubleshooting Cheat Sheet

                             Command                                                            Purpose

systemctl status your.service                          Check status

journalctl -u your.service                                  View logs

journalctl -u your.service -f                          Follow logs live

systemctl restart your.service                       Restart the service

systemctl stop your.service                               Stop the service

systemctl enable your.service                         Start on boot


Wrap-up: Why This Matters

Turning your Python script into a Linux service:

  • Makes it run reliably in the background
  • Auto-recovers on failure
  • Starts on boot without cron hacks
  • Gives you logs and tracebacks
  • Feels professional

You’re no longer just scripting — you're deploying like a real engineer.


Conclusion

Turning a Python script into a proper Linux service isn’t just about convenience — it’s about reliability, automation, and professionalism.

By wrapping your script with systemd, you gain:

  • Automatic recovery when things go wrong
  • Clean logging through journalctl or custom files
  • Environment isolation with virtualenv
  • Startup control without messy cron hacks

This approach is especially useful for long-running automation tasks, log collectors, monitoring tools, or background data jobs — the kind of scripts that are too important to silently fail.

It might take an extra 10 minutes to create the unit file and wire up logging, but the peace of mind (and fewer weekend SSH sessions) is absolutely worth it.

If you found this helpful, share it or leave a comment.

 

Amit Pisal

Hewlett Packard Enterprise (PS-GCC)



I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo