Tips for filtering specific processes using Python

Preface

In an operating system, a process is an instance of a running program. Sometimes we need to filter and manage specific processes, such as finding a process with a specific name, or filtering based on the properties of the process. If it is a Linux system, we have several performance monitoring commands that can be viewed in real time, and different commands can be used to filter out the results. If it is a Windows system, the command line is not so convenient, but Python provides a variety of ways to monitor the process. Filtering and operations, this article will introduce how to use Python to filter specified processes, and show some practical techniques and methods.

psutilFind processes using libraries

psutilIt is a powerful cross-platform library that can easily obtain system process and system utilization information. We can use it psutilto list all processes and filter based on the properties of the process.

Here is an example showing how to use psutilthe library to find a process of a specific name:

import psutil

def  find_process_by_name ( process_name ):
     for proc in psutil.process_iter([ 'pid' , 'name' ]):
         if proc.info[ 'name' ] == process_name:
             print ( f"Find process ' {process_name} ', PID for {proc.info[ 'pid' ]} " )

# Example usage 
find_process_by_name( 'chrome.exe' )

In this example, find_process_by_namethe function accepts a process name as a parameter, then iterates through all processes in the system, finds the process that matches the specified name and prints its PID.

Use the subprocess module to execute system commands

In addition to using third-party libraries psutil, we can also use subprocessmodules to execute system commands to find processes. On Linux and Unix systems, you can use psthe command to list processes and then parse the results through Python.

Here is an example of using subprocessa module to execute psa command:

import subprocess

def  find_process_by_name_linux ( process_name ):
    command = f"ps aux | grep ' {process_name} '" 
    result = subprocess.run(command, shell= True , stdout=subprocess.PIPE, text= True )
    output = result.stdout.strip()
    if output:
         print ( "Process found:" )
         print (output)
     else :
         print ( f"No process with name ' {process_name} ' found " )

# Example usage 
find_process_by_name_linux( 'chrome' )

In this example, find_process_by_name_linuxthe function executes ps aux | grep '{process_name}'the command and then parses the command output to find a process with the specified name.

Combine conditional filtering process

In addition to finding processes by name, we can also filter processes based on other criteria, such as filtering based on the process’s CPU utilization or memory usage. This is very effective when we find processes with high resource usage.

Here is an example that demonstrates how to filter processes based on their CPU utilization:

import psutil

def  find_high_cpu_processes ( threshold ):
     for proc in psutil.process_iter([ 'pid' , 'name' , 'cpu_percent' ]):
         if proc.info[ 'cpu_percent' ] > threshold:
             print ( f"process' {proc.info [ 'name' ]} 'CPU usage exceeds threshold {threshold} %" )

#Example usage 
find_high_cpu_processes( 50.0 )

In this example, find_high_cpu_processesthe function iterates through the processes in the system, finds those whose CPU utilization exceeds a specified threshold, and prints their names.

Summarize

Using Python to filter specified processes is one of the important tasks in managing and monitoring the system. This article introduces several methods to filter and manage processes, including using psutillibraries, executing system commands, and filtering processes based on conditions. Readers can choose the appropriate method to apply in actual scenarios based on actual needs and operating system environment.

Leave a Reply

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