A Comprehensive Guide to Displaying Running Python Processes on Linux

Dec 01, 2025 · Programming · 14 views · 7.8

Keywords: python | linux | process management

Abstract: This article provides a detailed guide on how to display running Python processes on Linux systems, focusing on the use of the ps command and its integration with Python programming. It explains the core concepts, offers code examples, and discusses alternative methods for process management.

Introduction

In Linux-based systems, such as CentOS, managing running processes is a common task for system administrators and developers. Specifically, identifying Python processes can be crucial for debugging, monitoring, or resource management.

Using the ps Command

The ps command, short for process status, is a standard Unix utility for displaying information about running processes. To list all processes and filter for Python, the command ps -ef | grep python is widely used. Here, -e selects all processes, and -f provides a full-format listing.

Code Example in Python

Instead of directly using shell commands, Python can be employed to programmatically retrieve process information. The subprocess module allows executing system commands and capturing their output.

import subprocess

def get_python_processes():
    result = subprocess.run(['ps', '-ef'], capture_output=True, text=True)
    processes = result.stdout.splitlines()
    python_processes = [p for p in processes if 'python' in p]
    return python_processes

if __name__ == "__main__":
    processes = get_python_processes()
    for p in processes:
        print(p)

This code runs the ps -ef command and filters the output for lines containing "python".

Alternative Methods

Other commands, such as pgrep -lf python, can also be used. pgrep searches for processes by name and -l lists the process name along with the PID, while -f matches the full command line.

Conclusion

Displaying running Python processes on Linux is straightforward using commands like ps -ef | grep python. For more programmatic control, Python's subprocess module provides a flexible solution. Understanding these methods enhances system management capabilities.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.