Linux show cpu usage

How can I determine the current CPU utilization from the shell? [closed]

How can I determine the current CPU utilization from the shell in Linux? For example, I get the load average like so:

Just FYI, the load average is not the CPU usage although it may indirectly show the CPU usage. It’s a number showing how many processes are out there running but waiting for resources, be it CPU or disk or something else.

can’t answer the question cause it’s closed but have a look at vmstat , as a bonus it works on BSDs as well.

7 Answers 7

Linux does not have any system variables that give the current CPU utilization. Instead, you have to read /proc/stat several times: each column in the cpu(n) lines gives the total CPU time, and you have to take subsequent readings of it to get percentages. See this document to find out what the various columns mean.

Because Space’s answer doesn’t work. It returns the average CPU usage since process start and not the current CPU usage. This answer is the correct one.

You can use top or ps commands to check the CPU usage.

using top : This will show you the cpu stats

using ps: This will show you the % cpu usage for each process.

ps -eo pcpu,pid,user,args | sort -r -k1 | less 

Also, you can write a small script in bash or perl to read /proc/stat and calculate the CPU usage.

We tried, and «top -b -n 1» gives very inaccurate results. «top»‘s first iteration is very approximative, can give 10 percent instead of 60.

top’s calculation of %CPU is different from ps’s calc of %CPU. see this answer unix.stackexchange.com/questions/58539/…

The command uptime gives you load averages for the past 1, 5, and 15 minutes.

Unfortunately I need it to exit right away so I can parse the output. I’m hoping there is a file in /proc that I can read.

This will be something like this:

cpu 55366 271 17283 75381807 22953 13468 94542 0 cpu0 3374 0 2187 9462432 1393 2 665 0 cpu1 2074 12 1314 9459589 841 2 43 0 cpu2 1664 0 1109 9447191 666 1 571 0 cpu3 864 0 716 9429250 387 2 118 0 cpu4 27667 110 5553 9358851 13900 2598 21784 0 cpu5 16625 146 2861 9388654 4556 4026 24979 0 cpu6 1790 0 1836 9436782 480 3307 19623 0 cpu7 1306 0 1702 9399053 726 3529 26756 0 intr 4421041070 559 10 0 4 5 0 0 0 26 0 0 0 111 0 129692 0 0 0 0 0 95 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 369 91027 1580921706 1277926101 570026630 991666971 0 277768 0 0 0 0 0 0 0 0 0 0 0 0 0 ctxt 8097121 btime 1251365089 processes 63692 procs_running 2 procs_blocked 0 

Источник

How to Check and Monitor CPU utilization on Linux

As a Linux administrator, it’s important to keep an eye on how your server (or servers) is performing. One way to measure its performance is to track the CPU usage. This will give you insight into the performance of the system as well as show how the hardware resources are being divided up across the various running services.

Читайте также:  Недостаточно свободного места linux

In this guide, we’ll go over a few methods to check and monitor the CPU utilization on a Linux system. Whether you are in charge of a server or just your personal desktop, the computer’s CPU usage is useful information that’s easy to acquire.

In this tutorial you will learn:

  • How to check CPU usage with top
  • Understanding the output from top and htop
  • Monitor CPU usage with systat package
  • How to configure CPU monitoring alters

How to Check and Monitor CPU utilization on Linux

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu, Debian, CentOS, RHEL, Fedora
Software Apache Webserver
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to check CPU usage with top

A great way to check the current CPU usage is with the top command. A lot of the output from this command is rather complex, but it gives very granular information about how the CPU is being utilized on a system.

This will open up a display in the terminal that has a live view of services running on the system, the amount of system resources each of those services are using, as well as a summary of the system’s CPU utilization, among other information.

Checking the current CPU usage with top command

NOTE
The top command mostly works the same across all Linux distributions, although there are some variants which may display the information a little differently – in a different order, for example.

The top program installed on Ubuntu systems comes from the procps-ng package, and is the one we will be working with in this article. To check what variant your system is running, use the -v flag with top :

The display window from the top command is not very user friendly at first because of the sheer amount of information and all the terminology and abbreviations used. We’ll cover everything you need to know below, so you can interpret the data from top.

The first line shows (in order): system time, system uptime (how long since last reboot), number of active user sessions, and the system’s load average. The load average is particularly relevant for us, as it sheds some light on the system’s CPU usage over time.

Load average

There are three numbers given for the load average. The numbers are the average load over 1, 5, and 15 minutes, respectively. Think of these numbers as percentages – a load of 0.2 means 20%, and a load of 1.00 means 100%.

That should be easy enough to understand, but you may also see load averages greater than 1.00. This is because load average is not a direct measurement of CPU usage, but how much “work” (load) your system is trying to process. For example, a value of 2.50 means that the current load is 250%, and also indicates that the system is overloaded by a whopping 150%.

Читайте также:  Linux вывод содержимого файла экран

The second line of top is pretty self-explanatory and displays the number of tasks running on the system, as well as the current state they’re in.

The third line is where we find our CPU usage, with some detailed statistics that take a little knowledge to interpret.

CPU usage from top

  • us: Percentage of CPU time spent in user space (running user-spawned processes).
  • sy: Percentage of CPU time spent in kernel space (running system processes).
  • ni: Percentage of CPU time spent on running processes with a user-defined priority (a specified nice value).
  • id: Percentage of CPU time spent idle.
  • wa: Percentage of CPU time spent on waiting on I/O from hardware. Example: waiting for a hard drive to finish reading data.
  • hi: Percentage of CPU time spent processing hardware interrupts. Example: the network card (or any piece of hardware) interrupting the CPU to notify it that new data has arrived.
  • si: Percentage of CPU time spent processing software interrupts. Example: a high priority service interrupting the CPU.
  • st: Percentage of CPU time that was stolen from a virtual machine. Example: the CPU needed to “steal” resources from a virtual machine in order to process the physical machine’s workload.

The next two lines are dedicated to memory information, and aren’t relevant for monitoring CPU usage. Below that, there’s a list of running processes and a column titled %CPU, which contains the current CPU usage of each process listed.

CPU usage percentage

This gives a quick view of which services are chewing up the most CPU resources. You can quit top any time by pressing ‘q’.

Making top simpler

Since the top command shows a lot of detailed information, it’s not an ideal method for getting a quick glance at CPU utilization; however, top gives us a few options to streamline the output and spare some of the complex details.

While top is running, you can press the ‘t’ key to cycle through some different views, and get a simpler output of the CPU usage:

CPU usage graph

Another option is to use htop , which is similar to top but geared more towards normal tasks. You can use your package manager to install it.

After it’s installed, just type htop to open it.

htop command

As you can see from the screenshot above, the output from htop is more concise and better suited than top for simple measuring of CPU usage.

You can exit this screen the same way as top, by pressing ‘q’.

More ways to check CPU utilization

There are a few more tools we can use to check CPU usage, and they’re contained in the sysstat package. You will have to install this package in order to use the commands.

$ sudo apt-get install sysstat

Once the sysstat package is installed, you will have access to the mpstat command. This shows a lot of the same information as top , but in a concise, one-time output.

user@ubuntu1:~$ mpstat Linux 5.0.0-23-generic (ubuntu1) 01/16/2020 _x86_64_ (1 CPU) 02:31:05 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 02:31:05 AM all 1.41 0.05 0.40 0.04 0.00 0.00 0.00 0.00 0.00 98.09

Another command that comes with this package is sar . It’s most useful when coupled with a number in the command. This allows you to specify how often (in seconds) the sar command should output information about CPU utilization.

Читайте также:  Kali linux установка xterm

For example, to check CPU usage every 4 seconds:

The output will look like this, and output a new line every 4 seconds:

user@ubuntu1:~$ sar 4 Linux 5.0.0-23-generic (ubuntu1) 01/16/2020 _x86_64_ (1 CPU) 02:33:24 AM CPU %user %nice %system %iowait %steal %idle 02:33:25 AM all 9.09 0.00 0.00 0.00 0.00 90.91

To make sar stop after a certain number of outputs, specify a second number. For example:

This will make sar check the CPU usage every 2 seconds, 5 times. It will also show the average of all 5 of its probes at the end of the output.

Graphical monitoring

If you are using a desktop client or have a GUI installed, there should be a graphical tool for monitoring system usage. Ubuntu uses Gnome by default as its desktop environment, and the command to launch the system manager is:

This will open a window similar to Window’s task manager, where you can sort processes by their CPU usage. Other distributions and desktop environments should have a similar tool.

Graphical monitoring with GNOME System Monitor on Ubuntu 20.04 Desktop

How to configure monitoring alerts

There are a lot of different ways to code a script that monitors CPU usage. In this part of the guide, we’ll go over one possible script where CPU usage is monitored every minute, and we’ll configure it to send an email when CPU usage gets high.

#!/bin/bash CPU=$(sar 1 5 | grep "Average" | sed 's/^.* //') CPU=$( printf "%.0f" $CPU ) if [ "$CPU" -lt 20 ] then echo "CPU usage is high!" | sendmail admin@example.com fi

This script uses sed to grab the average CPU idle percentage from sar . Then, it uses an if function to check if the idle percentage is below a certain number, and will send an email to the administrator if it is. In this case, it’s configured for 20% – in other words, if CPU usage is beyond 80%, the administrator gets an email.

The script can be tweaked as needed, like if you want it to echo a warning to the terminal or record to a log file instead of sending an email with sendmail .

Of course, you’d need to call this script from cron if you want it to run routinely.

To run it every minute, you’d write this line:

Conclusion

In this article, we saw how to check and monitor CPU utilization on a Linux system. We learned about multiple tools that can help us with the monitoring, and also learned how to set up usage alerts so we can be notified when CPU utilization is high.

Using the various methods from this guide, you’ll always know the best tool for keeping tabs on your system usage – whether you need detailed information or just need to quickly see how your system is allocating its CPU.

Comments and Discussions

Источник

Оцените статью
Adblock
detector