Software & AppsOperating SystemLinux

How To Get Real-Time CPU Usage in Command Line: Tips and Tricks

Ubuntu 17

Monitoring the CPU usage of your system is crucial for optimal performance. In this article, we will delve into how you can get real-time CPU usage directly from the command line. We will be using the sysstat package, which provides the mpstat and pidstat commands for detailed CPU statistics.

Quick Answer

To get real-time CPU usage in the command line, you can use the mpstat and pidstat commands from the sysstat package. The mpstat command provides overall CPU usage, while the pidstat command provides per-process CPU usage. Both commands can be used with the awk command to extract the CPU usage from their output.

Installing the Sysstat Package

Before we can use mpstat and pidstat, we need to ensure that the sysstat package is installed on our system. You can do this by running the following command:

sudo apt install sysstat

This command uses the sudo function to run the command as an administrator, apt to handle the package, and install to download and install the sysstat package.

Using the Mpstat Command

Once the sysstat package is installed, we can use the mpstat command to get real-time CPU usage. The command is as follows:

mpstat 1 1 | awk '/Average:/ {print 100 - $NF}'

Here, mpstat is the command that provides detailed CPU statistics. The two parameters 1 1 specify the interval and count respectively. This means the command will run every second (1 second interval) and will run only once (1 count).

The awk command is a powerful text-processing command. Here, it is used to extract the average CPU usage from the output of the mpstat command. The /Average:/ part is a pattern that awk looks for in the output. {print 100 - $NF} is the action that awk performs when it finds the pattern. Here, $NF refers to the last field on the line, which is the idle CPU time. So, 100 - $NF gives us the CPU usage.

Using the Pidstat Command

Another option to get real-time CPU usage is the pidstat command. This command provides statistics for individual processes, including CPU usage. Here’s how you can use it:

pidstat -u 1 1 | awk 'NR==4 {print 100 - $8}'

In this command, pidstat is the command that provides per-process CPU usage. The -u option tells pidstat to display CPU utilization. The 1 1 parameters specify the interval and count, just like in the mpstat command.

The awk command here is used to extract the overall CPU usage from the output of the pidstat command. NR==4 is the pattern that awk looks for, which is the fourth line of the output. {print 100 - $8} is the action that awk performs when it finds the pattern. Here, $8 refers to the eighth field on the line, which is the idle CPU time. So, 100 - $8 gives us the CPU usage.

Conclusion

Monitoring CPU usage is crucial for maintaining system performance. With the mpstat and pidstat commands, you can get real-time CPU usage directly from the command line. Whether you need overall CPU usage or per-process usage, these commands have you covered. As always, remember to check the man pages (man mpstat or man pidstat) for more information on these commands.

How often does the `mpstat` command update the CPU usage?

The mpstat command updates the CPU usage every second by default. You can change the update interval by specifying the desired interval in seconds as the first parameter of the command. For example, mpstat 5 will update the CPU usage every 5 seconds.

Can I use the `mpstat` command to monitor CPU usage on remote machines?

Yes, you can use the mpstat command to monitor CPU usage on remote machines. Simply specify the hostname or IP address of the remote machine as an argument when running the mpstat command. For example, mpstat -P ALL -I ALL -h remote_machine will display CPU usage statistics for all processors on the remote machine.

How can I interpret the CPU usage percentage displayed by `mpstat`?

The CPU usage percentage displayed by mpstat represents the amount of time the CPU was busy during the specified interval. A value of 100% means the CPU was fully utilized, while a lower percentage indicates idle time. Keep in mind that the CPU usage percentage is an average over the specified interval and may fluctuate over time.

Can I use the `pidstat` command to monitor CPU usage for specific processes?

Yes, you can use the pidstat command to monitor CPU usage for specific processes. Simply specify the process ID (PID) or the name of the process as an argument when running the pidstat command. For example, pidstat -p 1234 will display CPU usage statistics for the process with PID 1234.

Can I save the output of the `mpstat` or `pidstat` command to a file?

Yes, you can save the output of the mpstat or pidstat command to a file by using the output redirection feature of the command line. Simply append > filename.txt to the end of the command, replacing filename.txt with the desired name of the file. For example, mpstat 1 1 > cpu_usage.txt will save the CPU usage statistics to a file named "cpu_usage.txt".

Leave a Comment

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