Originally Published: Tuesday, 12 June 2001 Author: Michael Stutz
Published to: learn_articles_firststep/General Page: 4/5 - [Printable]

The Linux Cookbook: Chapter 2, WHAT EVERY LINUX USER KNOWS

Today's Learn article derived from Chapter Two of the soon to be published Linux Cookbook contains a ton of useful information for the new user. Learn all kinds of useful commands and short-cuts for basic system operation, and then join author Michael Stutz as he answers your questions live in Linux.com Live! Linux.com would like to thank publisher Bill Polluck of No Starch Press for the free use of this material.

Listing System Activity  << Page 4 of 5  >>

2.7 LISTING SYSTEM ACTIVITY

When you run a command, you are starting a process on the system, which is a program that is currently executing. Every process is given a unique number, called its process ID, or "PID."

Use ps to list processes on the system. Some of the information it can display about a process includes process ID, name of command being run, username running the command, and how long the process has been running. By default, ps outputs 5 columns: process ID, the name of the terminal from which the process was started, the current status of the process (including 'S' for sleeping, meaning that it is on hold at the moment, 'R' meaning that it is running, and 'Z' meaning that it is a zombie process, or a process whose parent processes have died), the total amount of time the CPU has spent on the process since the process started, and finally the name of the command being run.

The following recipes describe popular usage of ps.

2.7.1 LISTING YOUR CURRENT PROCESSES

Type ps with no arguments to list the processes you have running in your current shell session.

  • To list the processes in your current shell session, type:
$ ps "Ret"
PID TTY STAT TIME COMMAND
193 1 S 0:01 -bash
204 1 S 0:00 ps
$

In this example, ps shows that two processes are running: the bash and ps commands.

2.7.2 LISTING ALL OF A USER'S PROCESSES

To list all the processes of a specific user, use ps and give the username to list as an argument to the '-u' option. While you can't snoop on the actual activities of other users, you can list the commands they are running at a given moment.

  • To list all the processes that user hasdt has running on the system, type:
$ ps -u hst "Ret"

NOTE: This command is useful for listing all of your own processes, across all terminals and shell sessions; give your own username as an argument.

2.7.3 LISTING ALL PROCESSES ON THE SYSTEM

To list all processes by all users on the system, use the 'aux' options.

  • To list all of the processes and give their usernames, type:

$ ps aux "Ret"

NOTE: There could be a lot of output--even single-user Linux systems typically have fifty or more processes running at one time--so you may want to pipe the output of this command through less for perusal (see Recipe 9.1 Perusing Text, page 111).

Additionally, use top to show a display of all processes on the system, sorted by their demand on the system resources. The display is continually updated with current process information; type q to stop the display and exit the program. This tool also displays the information about system runtime and memory that can be output with the uptime and free commands.

  • To display a continually updated display of the current system processes, type:

$ top "Ret"

2.7.4 LISTING PROCESSES BY NAME OR NUMBER

To list processes whose output contains a name or other text to match, list all processes and pipe the output to grep. This is useful for when you want to see which users are running a particular program or command.

  • To list all the processes whose commands contain reference to an 'sbin' directory in them, type: $ ps aux | grep sbin "Ret"
  • To list any processes whose process IDs contain a 13 in them, type: $ ps aux | grep 13 "Ret" To list the process (if any) that correspond to a process ID, give that PID as an argument to the '-p' option.
  • To list the process whose PID is 344, type: $ ps -p 344 "Ret"




Listing System Activity  << Page 4 of 5  >>