Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk5 min

How to Run a Python Server Monitor as a Background Service on Linux

Use a systemd unit to supervise a foreground Python server monitor, start it at boot, restart it after failures, and inspect its logs with journalctl.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

On a Linux machine that uses systemd, run your Python monitor as a foreground process managed by a .service unit. Systemd can start it now, launch it at boot, restart it after qualifying failures, and send its output to the journal—without requiring the script to daemonize itself or remain attached to a terminal.

Before you create the service

First, make sure the monitor runs correctly from a shell and remains in the foreground. A systemd service supervises the process, so the Python program does not need to fork into a background daemon. Use the exact interpreter and entry point you intend to configure in the unit; for a virtual environment, that means its Python executable rather than an assumed shell activation.

As an Amazon Associate I earn from qualifying purchases.

This procedure is for Linux hosts using systemd, not every Linux init system. The tutorial example uses systemd 229; behavior and available settings can vary by release. Check the host with systemctl --version and consult its local systemd manuals. Python’s command-line reference linked here is for CPython 3.14.8; other Python implementations may differ.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a system service or a user service

Choice Use it when Operational effect
System service The monitor should run for the machine, independently of a particular interactive login. Managed by the system instance of systemd. Set a deliberate runtime account; do not run as root unless the monitor’s required operations genuinely need that privilege.
User service The monitor belongs to one user’s systemd instance and its permissions and administration should be scoped to that user. Normally follows the user’s manager/session lifecycle. To keep the user manager running without a logged-in session, configure lingering with loginctl enable-linger for that user.

For a system service, create or select a dedicated account and ensure it can read the script, virtual environment, configuration, and certificates, and access only the files and network resources it needs. A user service may be simpler when machine-wide independence is not required.

Create the systemd unit

A unit is a plain-text, INI-style configuration file. The example below is a starting shape, not a tested configuration: replace the account, paths, and timing with values appropriate to the host and application. Place the file at /etc/systemd/system/server-monitor.service for a system service.

[Unit]
Description=Python server monitor

[Service]
Type=simple
User=server-monitor
Group=server-monitor
WorkingDirectory=/opt/server-monitor
ExecStart=/opt/server-monitor/venv/bin/python -u /opt/server-monitor/monitor.py
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
  • ExecStart= uses an absolute Python path and script path, avoiding dependence on the interactive shell’s PATH.
  • WorkingDirectory= sets a known current directory for code that uses relative paths or expects application files there.
  • User= and Group= specify the runtime identity for this system-service example. Check access to every required file and resource as that account.
  • -u asks Python for unbuffered standard streams so output is less likely to be delayed when it is sent to logging rather than a terminal.
  • Restart=on-failure requests a restart after qualifying failures; RestartSec=5 sets a five-second delay in this example. Neither setting fixes an application fault, and systemd can rate-limit repeated start attempts.

Systemd’s service-unit manual describes the available service settings and their semantics; consult the manual installed for the target host before relying on behavior from another release: systemd.service.

Load, start, and enable the monitor

Reload the system manager after creating or changing a unit. Starting and enabling are separate: start runs the service now, while enable configures its activation at boot.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Reload unit definitions: sudo systemctl daemon-reload
  2. Start the monitor now: sudo systemctl start server-monitor.service
  3. Inspect its current state: sudo systemctl status server-monitor.service
  4. Enable boot activation if wanted: sudo systemctl enable server-monitor.service

If you edit the unit later, run sudo systemctl daemon-reload again and restart the service so the running process uses the updated configuration. For a user unit, use the corresponding systemctl --user commands; unit placement and journal access are user-scoped as well.

Choose restart behavior deliberately

Restart=on-failure is a reasonable starting point for a long-running monitor that should return after an abnormal exit. A restart policy is not a substitute for handling persistent errors: a bad executable path, missing dependency, invalid configuration, or repeating application failure will still prevent a healthy run. Systemd’s start-rate limiting can also stop repeated attempts. Choose a delay and policy based on how the monitor fails and how operators will detect a broken service rather than assuming automatic restarts resolve the cause.

Read service output in the journal

The example sends standard output and standard error to system logging. Use these commands to inspect existing unit messages or follow new ones:

sudo journalctl -u server-monitor.service
sudo journalctl -f -u server-monitor.service

The first prints journal entries for the unit; the second follows new entries as they arrive. System journal visibility depends on permissions, so use an authorized account if access is denied. For a user service, use the corresponding user-unit journal selection, such as journalctl --user-unit server-monitor.service. See the journalctl manual for filters and access details.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python can buffer output when it is not writing to a terminal. If print() messages appear late, use the -u option shown above, set PYTHONUNBUFFERED=1, or configure the application’s logging to write appropriately to the journal. Python documents -u in its command-line reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common failures

  • Unit not found or edits appear ignored: verify the file’s location and unit name, then run sudo systemctl daemon-reload and inspect systemctl status server-monitor.service.
  • The process exits immediately or keeps restarting: inspect journalctl -u server-monitor.service. Then try the same interpreter and entry point manually as the service account; check paths, permissions, working directory, configuration, and dependencies.
  • There is no recent print() output: Python may be buffering streams because they are no longer attached to a terminal. Use unbuffered output or application logging directed to the journal.
  • The service is enabled but is not running now: enablement configures activation at boot; run systemctl start separately to launch it immediately.
  • A user service stops after logout: this matches the normal relationship between a user manager and its session. Use a system service if machine-independent operation is required, or configure lingering for the user manager.
  • Journal entries are inaccessible: check permissions and use an account authorized to read the system journal.

Handle network readiness in the application

Do not treat After=network.target as proof that the network is usable: ordering after that target does not guarantee the network is fully online. If the monitor needs a working network, build in appropriate connection timeouts and retries, and consult the distribution’s systemd guidance for any stronger ordering requirement.

Check the host’s documentation

Systemd unit behavior is version-specific, and the tutorial pattern cited here is based on systemd 229. Compare the example with the installed system’s systemd.service and journalctl manuals. The Python command-line link is for CPython 3.14.8; use the documentation for the interpreter installed on the host if it differs. For a general explanation of this workflow, see the Python/systemd service tutorial.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.