- Community Home
- >
- Software
- >
- Software - General
- >
- How I Turned My Python Script into a Linux Service...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday - last edited Wednesday
Wednesday - last edited Wednesday
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]
