Wednesday, February 17, 2010
4:32 AM

Useful command line little tricks (part 2)

In the first part of this series, we discussed a bit about the folder structure, an important element in understanding Linux foundations. Today I want to concentrate on commands specific to process management and monitoring.

PROCESS MANAGEMENT AND MONITORING

If you have used the process monitor under Ubuntu, you know how good an application it is, but there are very useful shell commands you can use. The top and ps commands are both very useful, certainly incorporating more information and requiring less system
resources. Simply type the following:

top

You will see a neat table usually displaying a system information summary and about 20 processes. As you will quickly realize, this command displays processes in real time, refreshing the list every few seconds. To begin with, let's take a quick look at that system information summary displayed on top of the list, which provides lots of useful information, as can be seen on the following picture:



Starting from the first line, from left to right, we see the command name, the system time and the amount of time since the system was booted. Moving on, we can see how many users are currently running processes in the system, and then the average of processes waiting to run split in three time periods: 1, 5 and 15 minutes. The next row displays info about system tasks. CPU information awaits on the next row, starting with current load, then showing how much of it is used by system, nice (low priority), iddle, and awaiting processes. The next two rows display memory information. Simply type "q" to quit the top command and return to the system prompt.

You can narrow down the output from top in order to more comfortably display the information you are after. There are a number of different ways to do so, here are some examples:

top | grep swiftfox

In this example, I am using pipes to filter the outcome from top using grep, which displays only rows containing the word "swiftfox". You could also display only those processes you are interested in, by process id.

top -p9999 -p1111 -p2222

The example above would limit the output from top to only the processes with ids 9999, 1111 and 2222. You can add as many process ids as you want.

Another very useful process related command is ps. When run on its own, it apparently returns useless information:

ps

Output would return something like this:

PID    TTY      TIME CMD
7705 pts/0 00:00:00 bash
14522 pts/0 00:00:00 ps

This is because it is only showing processes from the current virtual terminal, which runs two commands, bash and ps. In order to display all processes, use it as follows:

ps -A | less

Because of the length of the list returned, I am piping it to the less command, so the output can be scrolled throughout. Once again, type "q" to exit less and return to the prompt.

ps -C swiftfox

In the example above I use the -C parameter to show processes by process name. In this case, I am using the process "swiftfox". Here's the resulting output:

PID    TTY      TIME CMD
532 ? 00:00:00 swiftfox

It is interesting to use the -f parameter, which displays full format. Combined with the -A (or -e) parameter, we get all of the processes in such format. Once again, we can benefit from piping output into the grep command to narrow down its input.

ps -Af | grep ps

The command above would return the following:

shred     7705  7702  0 20:57 pts/0    00:00:00 bash
shred 17920 7705 0 22:09 pts/0 00:00:00 grep bash

Finally, let's see how to terminate processes with the kill command.

sudo kill 9999

The example above would send a terminate signal to the application whose process id is 9999. This is equivalent to using the parameter 15, like this:

sudo kill -15 9999

Once again, this would send a terminate signal to the application, which would be handled at application level. There is a more extreme option, which terminates the process at kernel level. This option should only be used as a last resort if the default option does not work:

sudo kill -9 9999

And that is it for this second part of this series, specifically covering process management commands.

Enjoy!

0 comments:

Post a Comment