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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To restart a Python process after it exits without paying for a supervisor, a small watchdog can check for it and launch it again. The hard part is identifying the right process: a broad name or command-line match can mistake an unrelated process for the one you want. If your Python program launched the child, track it with subprocess.Popen; for an unattended service, use systemd’s restart policy instead of relying on a custom polling loop.

Choose how to identify the process

There are two different tasks that can look like “check whether Python is running”: checking a child process your script launched, and searching the system for a process that may have been started elsewhere.

Approach What it tracks Best fit Main limitation
subprocess.Popen A child launched by the current Python process, through its PID and return state Your watchdog starts and restarts the worker itself The parent must retain and manage the child process object
pgrep or /proc Processes selected by name or command line The target is independent of the watchdog Loose matches can select the wrong process
systemd Restart= A process managed as a systemd service An unattended service that should restart after specified exits or failures Restart behavior depends on the configured policy and exit condition
systemd WatchdogSec= A service that sends periodic liveness notifications The application can report that it is still responsive It requires regular watchdog notifications; it is not just a process-existence check

Python’s subprocess documentation describes the child PID and the poll(), wait(), and communicate() methods for checking or collecting a child’s completion state. For a process your script owns, this is more precise than searching every process on the machine.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Use Popen when Python starts the worker

Keep the Popen object and poll it. If poll() returns None, the child is still running; if it returns an exit status, the child has ended and can be restarted. A basic loop has this shape:

#1 Best Overall
Sale
GMKtec G3S Mini PC Intel N95 Processor (Up to 3.4GHz) 8GB RAM 256GB M.2 SSD
  • 12th Intel Alder Lake N95 Processor – The GMKtec G3 S Mini PC is powered by the 12th Gen Intel N95 processor with 4 cores, 4 threads, 6MB cache and a burst frequency up to 3.4GHz. Compared with N100/N5105/N5100/N5095, the N95 delivers up to 36% overall performance improvement. Perfect for routine tasks, office work, and home entertainment, this compact mini desktop is more convenient than traditional bulky PCs.
  • 8GB RAM & 256GB SSD Storage – Pre-installed with 8GB DDR4 memory and a fast 256GB M.2 2242 SSD, the G3 S mini desktop offers quicker startup, smoother multitasking, and faster file transfers. Enjoy seamless performance whether you’re working on multiple applications, browsing, or streaming content.
  • Rich Interfaces & Connectivity – The G3 S mini computer comes equipped with USB 3.2 (up to 10Gbps), dual HDMI 2.0 (4K@60Hz), and a 3.5mm audio jack. With support for WiFi 5, Bluetooth 5.0, and Gigabit Ethernet (RJ45 1000MbE), it connects easily with monitors, projectors, printers, office equipment, and other peripherals, making it versatile for both home and business use.
  • Dual 4K Display Support – Featuring upgraded Intel UHD Graphics (up to 1000MHz), the G3 S supports 4K video playback and AV1 decoding for a smooth viewing experience. With dual HDMI outputs, you can connect two 4K@60Hz displays simultaneously, enabling efficient multitasking for work and entertainment.
  • GMKtec WARRANTY - GMKtec offers a 1-year limited GMKtec's warranty for each mini PC, starting from the date of the purchase. All defects due to design and workmanship are covered. With a professional after sales team always ready to attend to your needs, you can simply relax and enjoy your mini PC.
import subprocess
import time

command = ["/usr/bin/python3", "/path/to/worker.py"]

while True:
    child = subprocess.Popen(command)
    exit_status = child.wait()
    print(f"Worker exited with status {exit_status}; restarting in 5 seconds")
    time.sleep(5)

Replace the command with the executable and script path for your setup. This loop restarts after any exit, including a clean exit with status 0. If a normal exit should stop supervision, inspect exit_status and break or handle that status according to your application’s policy. A fixed delay avoids an immediate tight restart loop, but it does not limit repeated failures over longer periods.

Passing an argument list, as above, avoids invoking a shell. Python’s subprocess documentation warns that when you explicitly invoke a shell, your application is responsible for quoting whitespace and shell metacharacters correctly. Do not build shell commands by interpolating untrusted text.

Rank #2
NIMO AI NAS, Agentic Computer Mini PC and AI Server, Intel Core Ultra 5 320 (up to 4.6 GHz, beat AI 5 340) up to 132TB ZFS Hybrid Storage, for 24hr AI Agent
  • High-Performance NAS with Powerful Procesor: Intel Core 5 320 is ideal for small offices, & More. You can enjoy smooth performance and seamless collaboration, while making use of advanced features like Docker and virtual machines. It works semalessly across every device inluding Windows, macOS, Linux, iOS, Android or Google services and so on.
  • Better Way to Store Than External Drives: NAS offers centralized storage, automatic backups, remote access, and a wide range of RAID options for easy data recovery even if a drive fails. Massive Storage Capacity: Never worry about storage limits again. With up 144TB capacity, you can store 50 million 1MB photos or 98K 1.5GB movies,5 million 30MB songs! *Hard Drives not included.
  • Secure Private Cloud: Retain 100% data ownership with advanced encryption to protect your files. Flexible permission management makes it easy to protect your privacy when collaborating with others.
  • AI-Powered Photo Album: Automatically organizes your photos by recognizing faces, scenes, objects, and locations. It can also instantly remove duplicates, freeing up storage space and saving you time.
  • User-Friendly App: Simple setup and easy file-sharing on Windows, macOS, Android, iOS, web browsers, and smart TVs, giving you secure access from any device.

Use /proc or pgrep only when you do not own the child

If another program starts the target, a watchdog can search for it and launch it when no suitable match is found. A PID check such as whether /proc/<PID> exists answers whether that PID currently has a process entry; by itself it does not establish that the process is the intended Python program. PID values can also be reused after a process exits, so a saved PID is not a durable identity for a separately managed process.

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

pgrep is often simpler than manually scanning /proc, but its matching behavior matters. By default it matches process names, which Linux limits to 15 characters in /proc/PID/stat. Its -f option matches against the full command line from /proc/PID/cmdline, making a distinctive match possible but not automatically safe. See the pgrep(1) manual for the matching options and limits.

Rank #3
ASUS NUC 14 Pro Mini Desktop Computer Linux, Intel Ultra 7 155H (16C/22T, Up to 4.8GHz), 64GB DDR5 RAM 2TB PCIe SSD, Mini PC with Intel Arc GPU, Type-C, WiFi 6E, Thunderbolt 4, VESA Mount for Business
  • ✅ Next-Gen AI Mini PC with Linux Mint – Open Source Meets Power: ASUS NUC 14 Pro delivers cutting-edge performance with the latest Intel Core Ultra 7 155H (16C/22T) processor and Linux Mint pre-installed for a secure, open-source environment. Ideal for developers, AI researchers, and power users, this mini desktop combines efficiency and flexibility with Intel Arc graphics for stunning visuals and AI acceleration.
  • ✅ Linux Mint for Developers, Creators & Businesses: Enjoy a lightweight, stable, and privacy-focused operating system that’s easy to use and developer-friendly. Linux Mint ensures a clutter-free experience without unnecessary bloatware, offering powerful open-source tools for programming, virtualization, and cloud-native development. This linux mint mini pc is perfect for professionals seeking freedom and security.
  • ✅ Scalable Memory & Blazing-Fast Storage: With configurations from 16GB to 64GB DDR5 RAM (expandable up to 96GB) and 512GB–2TB M.2 2280 PCIe Gen4 x4 SSD, this Linux Mint ASUS NUC handles heavy workloads effortlessly. Optional SATA HDD (sold separately) support gives you extra storage for large projects, making it ideal for coding, AI model training, and big data processing without performance bottlenecks.
  • ✅ Advanced Cooling for 24/7 Operation: ASUS NUC 14 Pro is engineered for silent and efficient cooling. The aluminum fin design, dual copper heat pipes, and optimized airflow system keep your mini PC cool during intense workloads. Perfect for running Linux-based servers, development environments, or AI inference tasks 24/7 without overheating.
  • ✅ Ultimate Connectivity & Multi-Display Support: Packed with versatile ports—USB 3.2 Gen2 x 2 Type C, USB 3.2 Gen2 Type A, HDMI 2.1, Thunderbolt 4 & 2.5G Gigabit Ethernet—this Linux Mint mini desktop supports 8K or up to four 4K HDR displays, enabling seamless multitasking. With WiFi 6E and Bluetooth 5.3, it’s ideal for developers, creative professionals, and home offices. VESA mount-ready for space-saving setups. Plus, enjoy a free $99 wireless keyboard and mouse bundle to boost your workflow.
  • Prefer a specific executable path or distinctive command-line argument over a generic name such as python.
  • Check what the match returns before using it to trigger a restart; two legitimate processes may share a name or argument.
  • Account for the watchdog itself if its command line contains the same search text.
  • Do not treat a match as proof of health: it only indicates that a process met the search criteria.

A /proc-based watchdog can demonstrate the restart pattern, but a generic process search is not a robust substitute for managing a known child or configuring a service manager. The exact matching logic must fit the way the target is launched.

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

For an unattended service, configure systemd

When the Python program should run as a Linux service, systemd can manage its lifecycle and apply a restart policy. In a unit file, the relevant setting is Restart=; choose its value based on which exit and failure conditions should cause a restart. The official systemd.service(5) manual documents the available restart behavior.

Rank #4
AMD Ryzen™ AI Halo - Personal AI Desktop Computer - Developer Platform - Linux OS
  • Built for Local AI Development: AMD Ryzen AI Halo is designed for local AI development and inference, featuring 128GB unified memory and support for up to 200B parameter models to build and run intensive AI workloads locally.
  • 128GB Unified Memory: Features 128GB LPDDR5x unified memory at 8000 MT/s with 256 GB/s memory bandwidth, providing a shared memory pool across the CPU, GPU, and NPU to support larger AI models.
  • AMD Ryzen AI Max+ 395 Processor: Features 16 cores, 32 threads, and Zen 5 architecture, paired with AMD Radeon 8060S integrated graphics featuring 40 RDNA 3.5 compute units and an AMD XDNA 2 NPU with up to 50 TOPS.
  • Linux AI Developer Platform: Purpose-built for Linux-based AI development with full AMD ROCm software support and preloaded tools, models, and workflows optimized for local AI development.
  • Compact, Connected Design: Includes a 2TB M.2 SSD, 10GbE LAN, Wi-Fi 7, Bluetooth 5.4, USB-C connectivity, and HDMI 2.1b.

Systemd’s WatchdogSec= is a different mechanism. It expects the service to send regular sd_notify("WATCHDOG=1") messages; systemd treats missed notifications as a watchdog failure. A program that merely needs to be relaunched after exiting generally needs a restart policy, while a program that must report ongoing liveness needs to implement the notification mechanism too.

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

Pick the least fragile option

  • Your Python script launches the worker: keep its Popen object and inspect the child’s completion directly.
  • The target is independent and you only need a small example: use a narrow pgrep or /proc match, and verify that it cannot select the watchdog or an unrelated process.
  • The program must run unattended as a service: use systemd’s Restart= policy for process exits; add WatchdogSec= only if the application can send the required liveness notifications.

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.