Using The ps And top Commands
The Unix commands covered in this section: ps, grep, top The basic unit of execution in UNIX (and many other operating systems) is called a process. In the "Redirecting input/output" section of the tutorial, you were asked to think of a process as a program you run from the shell. This is not far from the truth in most cases. What you think of as a program often consists of a single process. Your login shell and all the commands you've used in this tutorial run as single processes. In some cases, however, what you think of as a single program (e.g., a web or database server) actually consists of multiple processes communicating with each other. In this section, you will learn how to list the processes currently running on the system using the ps and top commands. In the next section, you'll learn how to manipulate processes. Exercise 1.1 Use the ps command to see the processes associated with
the current shell.
Until you've learned how to run processes in the background, the output of ps is not terribly interesting, since there are no other processes besides the shell itself to display. However, by adding some options to ps, we can make things a bit more interesting. The "-e" option tells ps to list all processes running on the system, not just those associated with the current shell. The "-f" option creates a full listing, which includes more information about each process. Exercise 1.2 Use ps -ef to get a full listing of all processes on the system. Since there are generally many processes to be listed, you'll want to pipe the output into a pager, such as more.
% ps -ef | more
The ps command under most versions of BSD requires different arguments to produce a full listing. Instead of "ps -ef", you would enter "ps aux". Linux and many versions of UNIX support both the standard UNIX arguments (with a "-") and the BSD-style arguments (without a "-"). The output of ps varies somewhat between UNIX versions, but you will generally see something similar to the output shown above. Table 1.1 shows the meaning of each field displayed in the full listing for ps in the last exercise. Table 1.1 field description
In some cases, you may want to restrict the listing to a few processes -- perhaps all processes belonging to a given user, or all occurrences of a certain program. You can use the grep command to do the filtering. It prints lines matching a specified pattern in its input while discarding lines that don't match. Exercise 1.3 Create a pipeline using ps -ef to get a full listing of all processes and grep look for all occurrences of the pine e-mail program.
% ps -ef | grep pine
The top program also lists processes, but adds the ability to sort by various criteria (CPU utilization by default) and provides a continuously updated display. It also gives a useful summary of the status of the system, including memory and CPU utilization. Exercise 1.4 Use top to display the processes using the most CPU time. To exit top when you're done, press 'q'.
% top
PID USERNAME THR PRI NICE SIZE RES
STATE TIME CPU COMMAND
Note that top also displays additional fields not shown by ps, including the scheduling priority and "nice" level.
Have a Unix Problem
Unix Books :-
Return to : - Unix System Administration Hints and Tips (c) www.gotothings.com All material on this site is Copyright.
|