Monitoring CPU and Memory Usage of Single Process on Linux: Methods and Practices

Nov 14, 2025 · Programming · 12 views · 7.8

Keywords: Linux process monitoring | CPU utilization | memory management | ps command | top command | system performance

Abstract: This article comprehensively explores various methods for monitoring CPU and memory usage of specific processes in Linux systems. It focuses on practical techniques using the ps command, including how to retrieve process CPU utilization, memory consumption, and command-line information. The article also covers the application of top command for real-time monitoring and demonstrates how to combine it with watch command for periodic data collection and CSV output. Through practical code examples and in-depth technical analysis, it provides complete process monitoring solutions for system administrators and developers.

Importance and Background of Process Monitoring

In Linux system administration and performance optimization, monitoring resource usage of individual processes is a critical task. Whether troubleshooting performance bottlenecks, identifying resource leaks, or conducting capacity planning, accurate data on process CPU and memory usage is essential. Linux systems provide multiple tools for this purpose, each with unique advantages and suitable scenarios.

Using ps Command for Process Resource Monitoring

The ps command is the most fundamental and commonly used process viewing tool in Linux. Through specific parameter combinations, it can precisely retrieve resource usage information for specified processes:

ps -p <pid> -o %cpu,%mem,cmd

In this command:

It's particularly important to note that the CPU usage displayed by ps command represents the average since process startup, not real-time instantaneous values. While this calculation method may lack precision in certain scenarios, it remains valuable for long-term trend analysis.

Real-time Monitoring with top Command

For scenarios requiring real-time monitoring, the top command offers a better solution:

top -p <pid>

The top command continuously refreshes and displays resource usage for the specified process, including real-time CPU utilization. This dynamic updating feature makes it particularly suitable for interactive monitoring and troubleshooting.

For scripted and automated monitoring, top's batch mode can be utilized:

top -b -n 2 -d 0.2 -p 6962 | tail -1 | awk '{print $9}'

This complex command combination achieves:

Implementing Periodic Monitoring and Data Logging

Combining with the watch command enables easy implementation of periodic monitoring and data recording to CSV files:

watch -n 1 'ps -p <pid> -o %cpu,%mem,cmd --no-headers | sed "s/^ *//" >> process_stats.csv'

This solution executes the ps command every second and appends results to a CSV file. Through appropriate formatting, structured monitoring data can be generated for subsequent analysis and visualization.

Practical Considerations in Production Environments

In actual production environments, process monitoring requires consideration of additional factors. The case study mentioned in the reference article demonstrates effective process monitoring under resource-constrained conditions. When system CPU usage reaches its limit, traditional SSH access may become difficult, making pre-configured monitoring mechanisms particularly important.

A robust monitoring solution should include:

Advanced Monitoring Techniques and Script Examples

For more complex monitoring requirements, specialized monitoring scripts can be written:

#!/bin/bash
PID=$1
INTERVAL=1

while true; do
    timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    stats=$(ps -p $PID -o %cpu,%mem --no-headers)
    echo "$timestamp,$stats" >> process_monitor.csv
    sleep $INTERVAL
done

This script provides more flexible control, allowing customization of monitoring intervals, output formats, and addition of timestamp information.

Performance Impact and Best Practices

When selecting monitoring solutions, it's crucial to balance monitoring accuracy with system overhead. Overly frequent monitoring may itself impact system performance. Generally:

Conclusion and Future Perspectives

Linux systems provide rich tools and interfaces for process resource monitoring. From simple ps commands to complex script solutions, developers can choose appropriate monitoring strategies based on specific requirements. With the evolution of containerization and cloud-native technologies, process monitoring continues to advance, but fundamental principles and tools retain their value. Mastering these basic monitoring techniques remains an essential skill for any Linux system administrator and developer.

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.