10+ Practical ps - Command Usage Examples in Linux
Posted by Superadmin on February 27 2017 05:12:16

10+ Practical “ps” Command Usage Examples in Linux

This tutorial explains Linux “ps” command, options and its usage with examples.

ps command reports a snapshot of the current processes. This post describes “ps” command used in Linux along with usage examples and/or output. ps command displays information about a selection of the active processes.

 

ps also accepts several kinds of options:
– UNIX options, which may be grouped and must be preceded by a dash.
– BSD options, which may be grouped and must not be used with a dash.
– GNU long options, which are preceded by two dashes.

It displays the process ID (pid=PID), the terminal associated with the process (tname=TTY), the cumulated CPU time in [DD-]hh:mm:ss format (time=TIME), and the executable name (ucmd=CMD). Output is unsorted by default.

Synopsis:

ps [options]

Examples:


Options:

-a
List information about all processes most frequently requested: all those except process group leaders and processes not associated with a terminal.
-A
List information for all processes. Identical to -e, below.
-c
Print information in a format that reflects scheduler properties as described in priocntl.
The -c option affects the output of the -f and -l options, as described below.
-d
List information about all processes except session leaders.
-e
List information about every process now running.
-f
Generate a full listing.
-j
Print session ID and process group ID.
-l
Generate a long listing.
-L
Print information about each light weight process (lwp) in each selected process.
-P
Print the number of the processor to which the process or lwp is bound, if any, under an additional column header, PSR.
-y
Under a long listing (-l), omit the obsolete F and ADDR columns and include an RSS column to report the resident set size of the process. Under the -y option, both RSS and SZ will be reported in units of kilobytes instead of pages.
-g grplist
List only process data whose group leader’s ID number(s) appears in grplist. (A group leader is a process whose process ID number is identical to its process group ID number.)
-n namelist
Specify the name of an alternative system namelist file in place of the default. This option is accepted for compatibility, but is ignored.
-o format
Print information according to the format specification given in format. This is fully described in DISPLAY FORMATS. Multiple -o options can be specified; the format specification will be interpreted as the space-character-separated concatenation of all the format option-arguments.
-p proclist
List only process data whose process ID numbers are given in proclist.
-s sidlist
List information on all session leaders whose IDs appear in sidlist.
-t term
List only process data associated with term. Terminal identifiers are specified as a device file name, and an identifier. For example, term/a, or pts/0.
-u uidlist
List only process data whose effective user ID number or login name is given in uidlist. In the listing, the numerical user ID will be printed unless you give the -f option, which prints the login name.
-U uidlist
List information for processes whose real user ID numbers or login names are given in uidlist. The uidlist must be a single argument in the form of a blank- or comma-separated list.
-G gidlist
List information for processes whose real group ID numbers are given in gidlist. The gidlist must be a single argument in the form of a blank- or comma-separated list.

Examples:

1. List processes in the current shell (ps)

  sanfoundry->$ ps
 
  PID TTY          TIME CMD
 1747 pts/0    00:00:00 bash
 2793 pts/0    00:00:00 ps

2. List every process running currently on the system (ps -ef OR ps -aux)

 
  sanfoundry->$  ps -ef
 
  UID        PID  PPID  C STIME TTY          TIME CMD 
  root         1     0  0 13:28 ?        00:00:01 /sbin/init 
  root         2     0  0 13:28 ?        00:00:00 [kthreadd] 
  root         3     2  0 13:28 ?        00:00:00 [ksoftirqd/0] 
  root         4     2  0 13:28 ?        00:00:00 [kworker/0:0] 
  root         6     2  0 13:28 ?        00:00:00 [migration/0] 
  root         7     2  0 13:28 ?        00:00:00 [watchdog/0] 
  root         8     2  0 13:28 ?        00:00:00 [migration/1] 
  root         9     2  0 13:28 ?        00:00:00 [kworker/1:0] 
  root        10     2  0 13:28 ?        00:00:00 [ksoftirqd/1] 
  root        12     2  0 13:28 ?        00:00:00 [watchdog/1] 
  whoopsie  1096     1  0 13:29 ?        00:00:00 whoopsie 
  mysql     1103     1  0 13:29 ?        00:00:00 /usr/sbin/mysqld 
  root      1117     1  0 13:29 ?        00:00:00 cron 
  daemon    1118     1  0 13:29 ?        00:00:00 atd 
  root      1195     2  0 13:29 ?        00:00:00 [krfcommd] 
  ......
  user_name  4857  4676  0 11:58 pts/3    00:00:00 ps -ef

HERE:
* -e to display all the current processes.
* -f for full format ordering of commands
* ps -aux command is used for BSD machines
* ps -e command would have just listed all the processes but with lesser number of attributes.
* Ps -eF gives extra full format

3. List every process on the system using BSD syntax (ps aux)

 
  sanfoundry->$ ps ax
 
  PID TTY      STAT   TIME COMMAND
  1 ?          Ss     0:01 /sbin/init
  2 ?          S      0:00 [kthreadd]
  .......
  4883 pts/3    R+     0:00 ps ax

STAT CODES ARE AS
* S interruptible sleep (waiting for an event to complete)
* < high-priority
* N low-priority
* s is a session leader
* l is multi-threaded
* + is in the foreground process group

So for eg – STAT Ss means A sleep peocess which is leading session and first in the queue to come up for execution .

4. Print a process tree (ps -ejH)

   sanfoundry-&gt;$ ps -ejH
 
   PID  PGID   SID TTY          TIME CMD
   2     0     0 ?        00:00:00 kthreadd
   3     0     0 ?        00:00:00   ksoftirqd/0
   ....

5. Print every process running as root (ps -U root -u root u)

sanfoundry-&gt;$ ps -U root -u root u 
 
USER       PID %CPU %MEM    VSZ   RSS TTY STAT START   TIME COMMAND
root       1    0.0  0.0  24576  2484 ?    Ss   11:05   0:01 /sbin/init
root       2    0.0  0.0      0     0 ?    S    11:05   0:00 [kthreadd]

HERE : 
* RSS: Resident set size = the non-swapped physical memory that a task has used
* VSZ: Virtual memory usage of entire process
* VSZ *includes* RSS

6. List name of command with a specific pid (ps -p X -o comm=)

   sanfoundry-&gt;$ ps -p 4983 -o comm=
 
   bash

7. List all processes of a specific user (ps -u user_name1,user_name2…)

Here -u stands for the specific user

 
   sanfoundry-&gt;$ ps -u user_name 	
 
   PID TTY          TIME CMD 
   2174 ?        00:00:00 gnome-keyring-d 
   2186 ?        00:00:00 gnome-session 
   2223 ?        00:00:00 ssh-agent 
   2226 ?        00:00:00 dbus-launch 	
   .......
   3201 pts/2    00:00:00 ps

8. List Processes in a Hierarchy tree (ps -e … –forest)

We get a recursion tree of all the parent processes and child processes forked by it

   sanfoundry-&gt;$ ps -e --forest 
 
    PID TTY          TIME     CMD 
    2    ?        00:00:00    kthreadd 
    3    ?        00:00:00  \_ ksoftirqd/0 
    6    ?        00:00:00  \_ migration/0 
    7    ?        00:00:00  \_ watchdog/0 
    8    ?        00:00:00  \_ migration/1 
    9    ?        00:00:00  \_ kworker/1:0 
   10    ?        00:00:00  \_ ksoftirqd/1 
   11    ?        00:00:00  \_ kworker/0:1 
   12    ?        00:00:00  \_ watchdog/1 
   13    ?        00:00:00  \_ migration/2

9. Finding memory Leakage (ps aux –sort pmem)

A memory leakage happens when an application uses lots of memory. Usually a process always frees memory when it’s work is done , but there are some processes which are need to work continuously and these may lead to system crash because of indefinite use of memory .

 
   sanfoundry-&gt;$ ps aux --sort pmem 
 
   USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND 
   root         2  0.0  0.0      0     0 ?        S    23:04   0:00 [kthreadd]  
   root         3  0.0  0.0      0     0 ?        S    23:04   0:00 [ksoftirqd/0] 
   root         6  0.0  0.0      0     0 ?        S    23:04   0:00 [migration/0]  
   root         7  0.0  0.0      0     0 ?        S    23:04   0:00 [watchdog/0] 
   root         8  0.0  0.0      0     0 ?        S    23:04   0:00 [migration/1] 
   root         9  0.0  0.0      0     0 ?        S    23:04   0:00 [kworker/1:0] 
   root        10  0.0  0.0      0     0 ?        S    23:04   0:00 [ksoftirqd/1] 
   root        11  0.0  0.0      0     0 ?        S    23:04   0:01 [kworker/0:1] 
   root        12  0.0  0.0      0     0 ?        S    23:04   0:00 [watchdog/1] 
   root        13  0.0  0.0      0     0 ?        S    23:04   0:00 [migration/2]

This command shows all the processes and %MEM used by them in a sorted way.
To check the memory leakage in an application , run the command again and again if it shows raise in %MEM (OR RSS) of a particular command with time , then a memory leakage is confirmed for that particular application.
Then , note down the pid for that process and kill it ot halt it in order to restore and enhance CPU Performance .

10. List of threads of a Process/All-Processes (ps …-L…pid , tid)

Lists the threads running for a particular process :

    sanfoundry-&gt;$ ps -L 
 
    PID   LWP   TTY      TIME     CMD 
    2892  2892 pts/2    00:00:00  bash 
    3398  3398 pts/2    00:00:00  ps 
 
    sanfoundry-&gt; ps -L 2892 
    PID   LWP   TTY      STAT   TIME  COMMAND 
    2892  2892  pts/2    Ss     0:00  bash

11. List elapsed wall time for processes (ps ….. -o pid,etime=)

This command provides etime which provides the elapsed time since the process was started, in the form dd-hh:mm:ss.

    sanfoundry-&gt;$ ps -p 1 -o pid,etime= 
 
    PID            
    	1123       1-11:38:37

12. Customizing the ps output format (-o option)

  sanfoundry-&gt;$ ps -eo uname,ppid,pid,nlwp,pmem,time,args
 
  USER      PPID   PID NLWP %MEM     TIME COMMAND
  root         0     1    1  0.0 00:00:01 /sbin/init
  root         0     2    1  0.0 00:00:00 [kthreadd]
  root         2     3    1  0.0 00:00:00 [ksoftirqd/0]
  root         2     6    1  0.0 00:00:00 [migration/0]
  root         2     7    1  0.0 00:00:00 [watchdog/0]
  root         2     8    1  0.0 00:00:00 [migration/1]
  root         2    10    1  0.0 00:00:00 [ksoftirqd/1]
  whoopsie     1  1223    2  0.1 00:00:00 whoopsie
  daemon       1  1226    1  0.0 00:00:00 atd
  root         2  1236    1  0.0 00:00:00 [kvm-irqfd-clean]
  root         2  1251    1  0.0 00:00:00 [hci0]
  ...

Note : 
ps command is often used with grep command