EmmonsNet

Geek Stuff

Home Software Linux Watch exactly what your OS is doing in Linux
Watch exactly what your OS is doing in Linux PDF Print E-mail
Written by Frank Emmons   
Monday, 17 August 2009 23:42

BashI love all the great utilities that are available out of the box with Linux. With the power they put at my fingers tips, I feel pretty darned near omnipotent at times when I am in front of the command line. Especially when some of the simplest utilities easily lay bare everything that is going on with the OS for me to scrutinize.

Earlier this evening a friend of mine who less than six months into his Linux journey catches me on IRC asking how he can tell which processes are consuming the most system resources as it happens.

Now here is where a little CLI magic comes with with the help of handful of *nix commands and a pair of pipes.

To grab a snap shot of the processes currently running on your box, you would use the 'ps' command. It has a heck of a lot of options. You can read more about it here or type 'man ps' in your console.

But grabbing a snapshot is only part of the equation. We want to be able to order that list and maybe make that list specific to one user on the box. Additionally he wanted to be able to watch it as it happened.

For the Linux neophyte, this may seem like a tall order. Don't be discouraged. You are running Linux - you are now the master of your digital universe!

To order a list, we are going to use the 'sort' command. It's job is, as you might have guessed, to sort lines of text. Read more about it here or type 'man sort' in the console.

We want to make this list specific (or match) to certain user, so 'grep' is ideal. Again - read up on the grep man page or type 'man grep' at command line.

Okay, so how do we slap all this together and make it work? This is where 'watch' comes in with a couple of pipes ( | ). Watch is a utility that will execute a program periodically, executing in full screen. Type 'man watch' in the command line or go here to rtfm.

Here is how we weave it all together.

watch -n 1 "ps aux | sort -nk +4 | grep <username here>"

When we call watch, we are giving it the parameter of '-n 1' - or seconds between each time it executes the command(s) we give it after within the opening double quotes. In the double quotes we have 'ps aux' or show all processes running on the system followed by the pipe ( | ). Next, we are sorting the results of the 'ps', with 'sort -nk +4'. This sorts the data numerically on the forth column of each line returned. Followed by the second pipe ( | ), we have 'grep <username>', obviously swap <username> for the user you are trying to watch. Lastly the closing double quotes.

When you put it all together and run it, you then see all the processes running by the user desired and sorted by how much CPU they are using. The results displayed will refresh every second. You can exit out of it by hitting CTRL-C at anytime.

To take this a step further, you can also follow that 'grep <username>' with a ' | tail' before the closing quotes. This will give you only the last 10 lines dumped from the 'ps' and they will be the most CPU intensives processes.

You can use this trick to give you a pretty clear idea of when you have something running on your box that needs to die. Then you can always use something like my serial killer bash script to get the job done.

Last Updated on Tuesday, 18 August 2009 00:24
 
Share/Save/Bookmark