Linux cpu used by process

Show top five CPU consuming processes with `ps`

Why use ps when you can do it easily with the top command?

If you must use ps , try this:

ps aux | sort -nrk 3,3 | head -n 5 

If you want something that’s truly ‘top’esq with constant updates, use watch

watch "ps aux | sort -nrk 3,3 | head -n 5" 

it will be good to include the numeric sort, to skip the header of ps aux. ps aux | sort -nrk 3,3 | head -n 5

this spawns many processes which is more resource consuming and is also more prone to race condition, because when the piped data comes to head the process list may have already changed

@icedwater top reads the process list and display the result on its own without piping to any other process

The correct answer is:

So you can specify columns without interfering with sorting.

ps -Ao user,uid,comm,pid,pcpu,tty --sort=-pcpu | head -n 6 

Note for MAC OS X: In Mac OS X, ps doesn’t recognize —sort , but offers -r to sort by current CPU usage. Thus, for Mac OS X you can use:

ps -Ao user,uid,comm,pid,pcpu,tty -r | head -n 6 

This IS the correct answer. Thank you Facundo. I was trying to explain to others that you can use sort and pipe it, but ps also comes built in with an option for sorting and now I see you also use it which is great.

The 2nd command appears more useful. but the first one just seems to show bash (x2, or head as well) ps .

@Wilf The first one is for highlighting how to sort by cpu consumption without using the command sort , the second one shows how to specify columns without interfering with sorting. When explaining something.. it’s always better to be concise and explain one thing at a time.

I was asked a question today about why «ps -eo pcpu,pid,comm,pmem | sort -r k1» does not give correct answer since there are some lowest usages on top, but higher ones in the middle. But when I found your answer and try it, I deleted my question since your suggestion worked correctly. Thanks @FacundoVictor.

I don’t think ps is what you are looking for. Have you looked at the output from top ?

If you have GNU-Top, try using it’s batch mode to spit out a process list sorted by cpu usage and using head/tail to get the top 5 lines (the first 8 are headers):

top -b -n 1 | head -n 12 | tail -n 5 

The BSD top seems to behave differently and doesn’t have a non-interactive mode, so use one of the other ps based solutions.

In OS X, is top -o cpu -n 5 a way of achieving the same thing. Does anyone know? My top is different to your top .

You’re the one with the different top so you would be in a position to say. My top doesn’t have -o and -n sets the number of times it refreshes the display before quitting.

Читайте также:  Основы администрирования linux систем

Fair enough. I’ll get on a Linux box this afternoon and take a look. My top doesn’t seem to have a batch mode, which is quite limiting. There must be some way of piping top into other commands. I’ll do some research.

I mentioned the batch mode switch in my answer but it’s actually unnecessary for my top because it auto-detects being part of a pipe instead of an interactive session. Did you try just piping it without that?

Depending on your needs you may find this a little more readable:

ps -eo pcpu,pid,user,args --no-headers| sort -t. -nk1,2 -k4,4 -r |head -n 5 
 1.3 4 root [ksoftirqd/0] 1.1 9 root [ksoftirqd/1] 1.0 17606 nobody /usr/sbin/gmetad 1.0 13 root [ksoftirqd/2] 0.3 17401 nobody /usr/sbin/gmond 

(the fields are %CPU,PID,USER,COMMAND)

Just to note the subtle difference here; at least on my system, -o pcpu shows the process’s CPU use over its lifetime, not over the last second like ps usually does. There doesn’t seem to be any way to get the short-term CPU use using ps -o ; it’s always just the cpu time the process has used divided by the time the process has been running.

Quickest one liner I have found for this (note 6 because the header adds up):

In order to add a point to other valuable answers, I prefer:

It also prints the header, which is nice.

Here k is identical to —sort and c specifies CPU usage (alias %cpu ) field for sort, while — is for reverse sort.

You may add more specifiers separated by , , other possible specifiers are : %mem , args , bsdstart , pid , gid , uid . which you can find full list in STANDARD FORMAT SPECIFIERS section of man page. For example:

would print 10 processes with highest group id, internally sorted by memory usage.

Note that current versions of ps have sorting ability within them, based on field codes (given in the ps man page). The field code for processor usage is «c». You can use —sort c at the end of a ps command e.g. ps aux —sort c

This will put the process using the most cpu at the bottom of the list. Reverse order of the list by adding a minus to the field code used to sort e.g. ps aux —sort -c

The command line tool ps has its own sort option, so I prefer:

$ ps -eo pcpu,args --sort=-%cpu | head 

You can add the columns you want. See what other options are available via the ps man page.

Here is something I came up with as I found the original answer a bit too arcane. so, for bare bones, type

ps -e -o pid,cmd,%cpu,%mem --sort=-%cpu | head -n 6 

Let’s understand what each does. ps ofcourse shows a snapshot of current processes. -e shows every process on the system -o is to define the format we want the result in, as you can see we have specified the format as pid,cmd,%cpu,%mem , next —sort ofcourse, sorts. One important point to note here is that by default it sorts ascending. Also —sort needs the parameter to sort by, which we provide by -%cpu notice the — this is so that it sorts descending and we get the highest CPU usage first. Then we pipe this into head -n 6 which gives us the this

 PID CMD %CPU %MEM 5995 transmission-gtk 5.1 1.3 402083 /usr/lib64/firefox/firefox 4.2 6.7 978875 /usr/lib64/firefox/firefox 3.6 4.0 2982 /usr/bin/gnome-shell 2.7 3.0 2774 /usr/libexec/Xorg vt2 -disp 1.9 1.0 

Now that we understand the basics we can show off a little. For example you can use watch to update the list every 2 seconds like this

watch "ps -e -o pid,cmd,%cpu,%mem --sort=-%cpu | head -n 6" 

Or, for something fancier, you can install a python package called tabulate , type this in your terminal pip install tabulate , now you can really show off, using some sed fu etc

 ps -e -o pid,cmd,%cpu,%mem --sort=-%cpu | head -n 5 | tabulate -1 -f github | cut -f 2- -d "|" | sed '2s/----/ /' 

which gives this beautiful output —

 | PID | CMD | %CPU | %MEM | |--------|----------------------------|--------|--------| | 5995 | transmission-gtk | 5.1 | 1.3 | | 978875 | /usr/lib64/firefox/firefox | 4.5 | 4.1 | | 402083 | /usr/lib64/firefox/firefox | 4.2 | 6.7 | | 2982 | /usr/bin/gnome-shell | 2.7 | 3 | 

for ease of use and avoid typing this command over and over you can alias them into your .bashrc like this

alias top5="ps -e -o pid,cmd,%cpu,%mem --sort=-%cpu | head -n 6" 

after doing source .bashrc you can just type top5 . Or, you can just use htop and sort by %CPU htop also allows you to kill processes and much more.

Читайте также:  Топ linux на debian

Источник

How to Check CPU Utilization in Linux with Command Line

Understanding CPU processor usage is important for overall system-performance measurement. From Linux enthusiasts to system admins, knowing how to monitor CPU utilization in Linux from the command line is crucial.

This guide will walk you through several options to check Linux CPU usage.

Article on how to check CPU utilization in Linux from the terminal.

  • A Linux-based computer (e.g., Ubuntu and CentOS)
  • Access to a user account with sudo privileges
  • A command prompt (Ctrl-Alt-T in Ubuntu, Menu > Applications > Utilities > Terminal in CentOS)
  • (optional) A package installer, like apt or yum, usually included by default

Note: Use one of 5 available commands in Linux to check memory usage.

How To Check CPU Usage from Linux Command Line

top Command to View Linux CPU Load

Open a terminal window and enter the following:

The system should respond by displaying a list of all the processes that are currently running. It will also give a readout of users, tasks, CPU load, and memory usage.

This list can frequently change, as background tasks start and complete. One helpful switch is to launch top with the –i switch:

This hides all the idle processes, making it easier to sort through the list.

To quit the top function, press the letter q on your keyboard.

Some other useful commands while top is running include:

  • M – sort task list by memory usage
  • P – sort task list by processor usage
  • N – sort task list by process ID
  • T – sort task list by run time

To get assistance with top , you can press the letter h while it’s running. Or, you can enter the following at a command line:

This will display the manual page for the top command.

mpstat Command to Display CPU Activity

Mpstat is part of a software bundle called sysstat. Most RHEL-based distributions include this software package.

For Debian and Ubuntu systems, you’ll need to install the sysstat package.

In a terminal window, enter the following:

sudo apt-get install sysstat

Allow the process to complete.

If you’re running an older (4.x or older) version of CentOS or Red Hat derivative, you can use up2date to install sysstat:

sudo up2date install sysstat

For newer (5.x and later) installations of CentOS or Red Hat, sysstat can be installed using the following command:

Читайте также:  Linux vpn server mikrotik client

Once the process finishes, you can use the mpstat command in the terminal as follows:

The system will display usage for each processor (or processor core).

The first line is a set of column labels. The second line is the value for each column:

  • %usr – % CPU usage at the user level
  • %nice – % CPU usage for user processes labeled “nice”
  • %sys – % CPU usage at the system (Linux kernel) level
  • %iowait – % CPU usage idling waiting on a disk read/write
  • %irq – % CPU usage handling hardware interrupts
  • %soft – % CPU usage handing software interrupts
  • %steal – % CPU usage being forced to wait for a hypervisor handling other virtual processors
  • %guest – % CPU usage spent running a virtual processor
  • %idle – % CPU usage on idle time (no processes, and not waiting on a disk read/write)

You can add switches to the mpstat command.

The –P switch allows you to specify a single processor to report:

This would show you a report for the first processor (CPU 0).

This command would show you the total, like the basic mpstat command. It will also list processes by individual CPU.

The mpstat command only takes a snapshot of CPU usage.

To take a series of snapshots, use a number to indicate an interval and a second number to indicate the number of reports:

This example would generate 7 snapshots, each 5 seconds apart.

sar Command to Show CPU Utilization

The sar tool is a utility for managing system resources. It’s not limited strictly to CPU usage, but you can use the -u option to track CPU performance.

Use the following command to direct sar to monitor CPU usage at set intervals:

The –u option tells it to display CPU usage. The 5 indicates that it should display every 5 seconds. This will run indefinitely. To cancel, press Ctrl-C.

iostat Command for Average Usage

In a terminal, enter the following:

The system will display average CPU usage since the last boot. It will also display input/output load (disk read/write activity).

More information on iostat can be found on the Linux Manual pages.

Other Options to Monitor CPU Performance

Nmon Monitoring Tool

Nmon is a monitoring tool developed by Nigel Griffiths of IBM. To install Nmon on Ubuntu, enter the following:

To install to CentOS, enter the following:

The command to launch nmon is:

This will launch the utility, and display all the options. To view CPU usage, press the letter c. To switch back, press c again. For a list of commands, press h. To quit, press q.

Graphical Utility Option

Many server systems don’t waste processor cycles on a graphical user interface (GUI).

However, you may have a lightweight GUI, or you may be using a client Linux system. Some versions, like Ubuntu, have a built-in graphical monitoring tool.

To launch Ubuntu’s system monitor, enter the following in a terminal window:

This starts a task-manager-like application where you can monitor tasks and CPU usage.

Typically, GUI’s have a “task manager” or “system monitor” application. This can be used to monitor CPU usage in real-time.

There are many different methods to check CPU usage in Linux.

This guide outlines the primary methods using built-in Linux tools or third-party applications. These commands will help you track processor usage and performance of your system, giving you greater control.

Источник

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