Home  >  Article  >  Backend Development  >  Use Python script operations to monitor and manage processes in Linux

Use Python script operations to monitor and manage processes in Linux

PHPz
PHPzOriginal
2023-10-05 09:03:36738browse

Use Python script operations to monitor and manage processes in Linux

Title: Python script implements process monitoring and management in Linux

Abstract:
This article introduces how to use Python script to monitor processes in Linux systems and management. By writing Python scripts, we can easily implement process monitoring and management operations, including querying process information, starting new processes, stopping specified processes or stopping processes in batches, etc. Specific code examples will be given later. By studying this article, readers can master the basic methods of using Python scripts to monitor and manage Linux processes.

Keywords:
Python script, Linux process, monitoring, management, code examples

Introduction:
In the Linux system, the process is the basic unit running in the operating system. Process monitoring and management are of great significance to ensure the stable operation of the system and the reasonable allocation of resources. The traditional operation method mainly relies on command line tools, and the operation is not flexible and convenient enough. As a simple, easy-to-use, feature-rich programming language, Python provides powerful process monitoring and management capabilities, and can easily implement various operations.

1. Query process information
Python provides the psutil library, which can easily query and process process-related information. The following is a sample code that can query the PID, name, status and other information of the specified process.

import psutil

def query_process(process_name):
    for proc in psutil.process_iter(['pid', 'name', 'status']):
        if proc.info['name'] == process_name:
            print(f"PID: {proc.info['pid']}, Name: {proc.info['name']}, Status: {proc.info['status']}")

query_process("python")

By calling the psutil.process_iter() function, we can get the iterators of all processes in the current system, and then traverse to get the information of each process. By comparing the names of processes, we can filter out the processes we need to query. Here we take querying the Python process as an example.

2. Start a new process
Sometimes we need to start a new process through a Python script. Python's subprocess module provides corresponding functions. The following is a sample code:

import subprocess

def start_process(cmd):
    subprocess.Popen(cmd)

start_process("ls -l")

By calling the subprocess.Popen() function and passing in the corresponding command line instructions, you can start a new process. Here we take starting the ls -l command as an example.

3. Stop the process
In certain scenarios, we may need to stop the specified process. This function can be easily implemented using Python scripts. The following is a sample code:

import os

def stop_process(pid):
    os.kill(pid, signal.SIGTERM)

stop_process(1234)

Calling the os.kill() function, we can send a signal to the specified process to stop the process. Here we take stopping the process with PID 1234 as an example.

4. Stop processes in batches
When you need to stop multiple processes at the same time, it is more convenient to use Python scripts. The following is the sample code:

import psutil

def stop_all_processes(process_name):
    for proc in psutil.process_iter(['pid', 'name']):
        if proc.info['name'] == process_name:
            os.kill(proc.info['pid'], signal.SIGTERM)

stop_all_processes("python")

By traversing all processes, we can filter out the processes that need to be stopped and send a stop signal to them using the os.kill() function. Here we take stopping all Python processes as an example.

Conclusion:
This article introduces the basic method of using Python scripts to monitor and manage processes in Linux systems, and provides corresponding code examples. By writing Python scripts, we can easily implement operations such as querying, starting and stopping processes. Readers can further expand and apply it according to specific needs. By mastering these basic methods, we can more flexibly monitor and manage the processes in the system and improve the operating efficiency and stability of the system.

The above is the detailed content of Use Python script operations to monitor and manage processes in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn