How to Get the list of Running Processes using Python?


The operating system runs with hundreds of tasks/process at a single time. As a Python developer, this is important because often while dealing with programs that consume huge amounts of memory, we may want to delete some unimportant tasks. This article will explore how to get the running process list using Python.

Using psutil Module

The psutil module is a powerful cross−platform library for system monitoring and process management in Python. It provides a convenient and consistent API to access system−related information, such as CPU usage, memory utilization, disk usage, network statistics, etc. Being cross−platform, the same code applies to all the operating systems.

Example

In the following code, we first imported the psutil library in the code using the import statement of Python. Next, we used the process_iter method of Python. The method returns all the processes which are running in the system. Next, we used the for loop to iterate over the list and print all the running processes.

import psutil
processes = psutil.process_iter()
for process in processes:
    print(f"Process ID: {process.pid}, Name: {process.name()}")

Output

Process ID: 1, Name: systemd
Process ID: 2, Name: kthreadd
Process ID: 3, Name: rcu_gp
Process ID: 4, Name: rcu_par_gp
Process ID: 5, Name: slub_flushwq
Process ID: 6, Name: netns
.................................................
Process ID: 66499, Name: postgres
Process ID: 66500, Name: postgres
Process ID: 66501, Name: postgres
Process ID: 66614, Name: kworker/6:0-events

Using Subprocess And Platform Module

Subprocess and platform modules are two important modules in Python that deal with the operating system. The subprocess allows the coders to interact with the operating system and execute external commands and scripts. This module is particularly useful when running system commands or managing external programs within your Python code.

The platform module, on the other hand, gives us information on the platform and operating system on which the Python script is running. It offers methods and functions to retrieve details about the platform's name, version, architecture, and hardware information. The module is useful when we want to run scripts that are platform specific.

Example

We first imported the subprocess and platform module using the import statement in the following code. Next, we initialized an empty String named command. We used the system method of the platform to access the current operating system. We then updated the value of the command String depending upon that. Next, we used the check_output method of the subprocess to get the system info. We passed the command parameter to inform the method about the current operating system.

import subprocess
import platform
command = ""
if platform.system() == "Windows":
    command = "tasklist"
elif platform.system() == "Linux":
    command = "ps aux"
elif platform.system() == "Darwin":
    command = "ps aux"
output = subprocess.check_output(command, shell=True, text=True)
print(output)

Output

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0 168556 13420 ?        Ss   09:37   0:01 /sbin/init splash
root           2  0.0  0.0      0     0 ?        S    09:37   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   09:37   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   09:37   0:00 [rcu_par_gp]
root           5  0.0  0.0      0     0 ?        I<   09:37   0:00 [slub_flushwq]
root           6  0.0  0.0      0     0 ?        I<   09:37   0:00 [netns]
...............................................................................................

handle=0,i,13555718610975125658,12212836532179255023,262144
asifr      66779  0.0  0.0   2888   960 ?        S    11:07   0:00 /bin/sh -c ps aux
asifr      66780  0.0  0.0  21324  3548 ?        R    11:07   0:00 ps aux

Using wmi Module

The WMI(Windows Management Instrumentation) is a Python library to interact with only Windows−based systems. It provides a Python interface to access and manipulate WMI data, allowing you to retrieve information about hardware, software, processes, and more. With the WMI module, you can connect to the WMI service on a local or remote Windows machine and query the available WMI classes and their properties. This enables you to gather various system information, such as CPU usage, memory usage, disk space, network configuration, and installed software.

Example

In the following code, we first imported the wmi module. Next, we created an object using the WMI method. We named the object to be wmi_obj. Next, we used the Win32_Process method to get a list of all the processes running in the System. Please note that the code works only for Windows 32 machines. Next, we iterated through the list and printed the results.

import wmi
wmi_obj = wmi.WMI()
processes = wmi_obj.Win32_Process()
for process in processes:
    print(f"Process ID: {process.ProcessId}, Name: {process.Name}")

Output

Process ID: 0, Name: System Idle Process
Process ID: 4, Name: System
Process ID: 76, Name: Registry
Process ID: 244, Name: smss.exe
Process ID: 328, Name: csrss.exe
Process ID: 408, Name: wininit.exe
Process ID: 444, Name: csrss.exe
Process ID: 484, Name: services.exe
Process ID: 496, Name: lsass.exe
Process ID: 504, Name: lsm.exe
Process ID: 596, Name: svchost.exe
...

Conclusion

In this article, we have understood how to get the list of processes using Python. We have seen that Python allows cross−platform and platform−specific libraries to achieve the same. While the psutil, platform, etc., are cross−platform and can run on any system but libraries like wmi works only on the Windows system.

Updated on: 18-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements