SoFunction
Updated on 2025-03-01

How to monitor process performance using the pidstat command in Linux

1. Install the pidstat command

Check if the system is installedpidstatOpen the terminal and enter the following command to check whether it has been installed.pidstat

pidstat -V

If the version information is displayed, it means that it has been installed, you can skip the installation steps. If the command is not found, then continue to the next installation.

Update Package ManagerInstallationpidstatBefore, it is recommended to update the system's package manager to get the latest software package list. The following commands can be used:

For the basis ofDebianorUbuntuSystem:

sudo apt update

For the basis ofRed HatCentOSorFedoraSystem:

sudo yum update

Install the sysstat package pidstatyessysstatPart of the toolkit, so we need to installsysstatCome and getpidstat

For the basis ofDebianorUbuntuSystem:

sudo apt install sysstat

For the basis ofRed HatCentOSorFedoraSystem:

sudo yum install sysstat

Verify installationAfter the installation is completed, verifypidstatWhether it has been successfully installed:

pidstat -V

You should see an output similar to the following, indicatingpidstatInstalled successfully:

sysstat version: 11.7.3
pidstat version: 11.7.3

2. Use pidstat to monitor the process

pidstatMultiple indicators can be monitored, such as CPU usage, memory usage, I/O operations, etc. Next, we will explain in detail how to use different options to monitor processes.

1. Check CPU usage

To view CPU usage for all processes, use the following command:

pidstat

This will display the CPU usage for each process, and the output will include the following information:

  • PID: Process ID.

  • %usr: User-mode CPU usage.

  • %system: Kernel state CPU usage.

  • %CPU: Total CPU usage.

If you want to continuously monitor the CPU usage of the process for a period of time, you can use the following commands:

pidstat 2 5

This means that the data is refreshed every 2 seconds, showing 5 times in total. The refresh interval and number of times can be adjusted as needed.

2. Check the CPU usage of the specified process

To view the CPU usage of a particular process, assume that the PID of the process is1234, you can use the following command:

pidstat -p 1234

This will only show the CPU usage of the process.

3. Check memory usage

To view the memory usage of all processes, you can use-rOptions:

pidstat -r

This will display the following memory-related information:

  • minflt/s: Number of secondary page errors per second.

  • majflt/s: Number of main page errors per second.

  • VSZ: Virtual memory size.

  • RSS: Residence set size.

Similarly, if you want to continuously monitor the memory usage of a process for a period of time, you can specify the time interval and number of times:

pidstat -r 2 5

This means that memory usage is refreshed every 2 seconds, showing 5 times in total.

4. View I/O operations

To monitor the I/O operation of the process, you can use-dOptions:

pidstat -d

This displays the following information related to I/O operations:

  • kB_rd/s: The amount of data read from disk per second.

  • kB_wr/s: The amount of data written to disk per second.

  • kB_ccwr/s: The number of kilobytes written (due to cache).

You can also specify refresh intervals and times:

pidstat -d 2 5

This means that the I/O operation data is refreshed every 2 seconds, and a total of 5 displays are displayed.

5. View thread information

pidstatIt can also monitor the thread usage of each process, using-tOptions can display thread-level monitoring information:

pidstat -t

This displays thread details for each process, including thread ID (TID), thread CPU usage, etc.

6. Save monitoring data

If you want to save monitoring data to a file, you can use the redirector to write the output to the file. For example, save CPU usage tocpu_usage.txtmiddle:

pidstat 2 5 > cpu_usage.txt

This way you can view the data in the file later.

3. Combined with other options

You can use it in combinationpidstatMultiple options. For example, monitor the CPU, memory, and I/O usage of a particular process, assuming that the process ID is1234, you can use the following command:

pidstat -p 1234 -r -d

This will show the process1234CPU, memory and I/O usage.

4. Timed task monitoring

If you need to monitor the process for a long time, you can combine it withcronornohupCommand to use. For example, usenohupRun the following command to keep running in the background and save the output to

nohup pidstat 2 1000 >  &

This will record the system's process information every 2 seconds, lasting 1000 times, and record the output toin the file. You can passtail -f View log file updates in real time

This is the article about how to monitor process performance using the pidstat command in Linux. For more related content, please search for my previous article or continue browsing the related articles below. I hope you can support me in the future!