-
Check that the system has installed the
pidstat
Open a terminal and enter the following command to check if it is already installedpidstat
:pidstat -V
If the version information is displayed, it means that it is already installed and you can skip the installation step. If it prompts for a command that cannot be found, then continue to the next step of the installation.
-
Update Package Manager installed in
pidstat
Before doing so, it is recommended to update the system's package manager to get the latest list of packages. You can use the following command:For those who are based on
Debian
maybeUbuntu
of the system:sudo apt update
For those who are based on
Red Hat
、CentOS
maybeFedora
of the system:sudo yum update
-
Install the sysstat package
pidstat
besysstat
part of the toolkit, so we need to install thesysstat
to getpidstat
。For those who are based on
Debian
maybeUbuntu
of the system:sudo apt install sysstat
For those who are based on
Red Hat
、CentOS
maybeFedora
of the system:sudo yum install sysstat
-
Verify Installation After the installation is complete, verify that
pidstat
Whether or not it has been successfully installed:pidstat -V
You should see output similar to the following, indicating
pidstat
It has been installed successfully:sysstat version: 11.7.3
pidstat version: 11.7.3
II. Usepidstat
Monitoring processes
pidstat
Several metrics can be monitored, such as CPU utilization, memory usage, I/O operations, and so on. Next, we will describe in detail how to monitor processes using different options.
1. Viewing CPU Usage
To view CPU usage for all processes, use the following command:
pidstat
This displays the CPU utilization of each process and the output includes the following information:
-
PID
: Process ID. -
%usr
: User-state CPU utilization. -
%system
: Kernel state CPU utilization. -
%CPU
: Total CPU utilization.
If you want to continuously monitor the CPU usage of a process over a period of time, you can use a command in the following format:
pidstat 2 5
This means that the data is refreshed every 2 seconds and displayed 5 times in total. You can adjust the refresh interval and number of times as needed.
2. View the CPU usage of the specified process
To view the CPU usage of a particular process, assume that the process has a PID of1234
, you can use the following command:
pidstat -p 1234
This will only show the CPU usage of the process.
3. Viewing Memory Usage
To see the memory usage of all processes, you can use the-r
Options:
pidstat -r
This displays the following memory-related information:
-
minflt/s
:: Number of secondary page errors per second. -
majflt/s
: The number of main page errors per second. -
VSZ
: Virtual memory size. -
RSS
:: Resident set size.
Similarly, if you want to continuously monitor the memory usage of a process over a period of time, you can specify the time interval and the number of times:
pidstat -r 2 5
This means that the memory usage is refreshed every 2 seconds for a total of 5 times.
4. view I/O operations
To monitor the I/O operations of a process, you can use the-d
Options:
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 to cancel a write (due to caching).
You can also specify the refresh interval and number of times:
pidstat -d 2 5
This means that the I/O operation data is refreshed every 2 seconds and displayed 5 times in total.
5. Viewing thread information
pidstat
It is also possible to monitor thread usage per process, using the-t
option displays thread-level monitoring information:
pidstat -t
This displays thread details for each process, including the thread ID (TID
), thread CPU utilization, and so on.
6. Preservation of monitoring data
If you wish to save the monitoring data to a file, you can use a redirector to write the output to a file. For example, saving CPU usage to thecpu_usage.txt
Center:
pidstat 2 5 > cpu_usage.txt
This allows you to view the data in that file later.
III. Combining other options
You can combinepidstat
The following are some of the options for monitoring the CPU, memory, and I/O usage of a particular process. For example, to monitor the CPU, memory, and I/O usage of a specific process, assume the process ID is1234
, you can use the following command:
pidstat -p 1234 -r -d
This will show the process1234
CPU, memory, and I/O usage.
IV. Timed task monitoring
If you need to monitor the process for an extended period of time, you can combine thecron
maybenohup
command is used. For example, use thenohup
Run the following command to keep it running in the background and save the output to the
nohup pidstat 2 1000 > &
This will log the system's process information every 2 seconds for 1000 times and log the output to the file. You can add a new file to the file by using the
tail -f