Table of Contents

Linux Commands#System

ps - How to use ps Command in Linux With Examples

report a snapshot of the current processes.



ps options

ps --help

$ ps --help

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).
$ ps --help simple

Usage:
 ps [options]

Basic options:
 -A, -e               all processes
 -a                   all with tty, except session leaders
  a                   all with tty, including other users
 -d                   all except session leaders
 -N, --deselect       negate selection
  r                   only running processes
  T                   all processes on this terminal
  x                   processes without controlling ttys

For more details see ps(1).


man ps

# man ps
EXAMPLES
       To see every process on the system using standard syntax:
          ps -e
          ps -ef
          ps -eF
          ps -ely

       To see every process on the system using BSD syntax:
          ps ax
          ps axu

       To print a process tree:
          ps -ejH
          ps axjf

       To get info about threads:
          ps -eLf
          ps axms

SIMPLE PROCESS SELECTION
       a      Lift the BSD-style "only yourself" restriction, which is imposed upon the set of all processes when some
              BSD-style (without "-") options are used or when the ps personality setting is BSD-like.  The set of
              processes selected in this manner is in addition to the set of processes selected by other means.  An
              alternate description is that this option causes ps to list all processes with a terminal (tty), or to
              list all processes when used together with the x option.

       x      Lift the BSD-style "must have a tty" restriction, which is imposed upon the set of all processes when
              some BSD-style (without "-") options are used or when the ps personality setting is BSD-like.  The set of
              processes selected in this manner is in addition to the set of processes selected by other means.  An
              alternate description is that this option causes ps to list all processes owned by you (same EUID as ps),
              or to list all processes when used together with the a option.

OUTPUT FORMAT CONTROL
       u      Display user-oriented format.

       l      Display BSD long format.


OUTPUT MODIFIERS
       f      ASCII art process hierarchy (forest).
       w      Wide output.  Use this option twice for unlimited width.


ps Examples

ps -ef

[root@localhost ~]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 00:01 ?        00:00:00 init [3]
root         2     1  0 00:01 ?        00:00:00 [migration/0]
root         3     1  0 00:01 ?        00:00:00 [ksoftirqd/0]
(abbr)

ps aux [Recommend Option]

You can check %CPU and %MEM.

[root@localhost ~]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.3  0.0  10348   688 ?        Ss   00:01   0:00 init [3]
root         2  0.0  0.0      0     0 ?        S<   00:01   0:00 [migration/0]
root         3  0.0  0.0      0     0 ?        SN   00:01   0:00 [ksoftirqd/0]
(abbr)

ps alx (Check NICE)

[root@localhost ~]# ps alx
F   UID   PID  PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4     0     1     0  15   0  10348   684 -      Ss   ?          0:00 init [3]
1     0     2     1 -100  -      0     0 migrat S<   ?          0:00 [migration/0]
1     0     3     1  34  19      0     0 ksofti SN   ?          0:00 [ksoftirqd/0]
(abbr)

ps auxf (To print a process tree)

[root@localhost ~]# ps auxf
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
(abbr)
root       319  0.2  0.1  10868   956 ?        S<s  00:08   0:00 /sbin/udevd -d
root      1102  0.0  0.1  10864   944 ?        S<   00:08   0:00  \_ /sbin/udevd -d
root      1103  0.0  0.1  10864   944 ?        S<   00:08   0:00  \_ /sbin/udevd -d
(abbr)
root      1120  0.4  0.8 100364  4168 ?        Ss   00:09   0:00  \_ sshd: root@pts/0
root      1122  0.2  0.3 108304  1900 pts/0    Ss   00:09   0:00      \_ -bash
root      1137  0.0  0.2 110336  1144 pts/0    R+   00:09   0:00          \_ ps auxf
(abbr)
root      1042  0.0  0.8 183796  4236 ?        Ss   00:08   0:00 /usr/sbin/httpd
apache    1048  0.0  1.0 185448  5300 ?        S    00:08   0:00  \_ /usr/sbin/httpd
apache    1049  0.0  1.0 185448  5300 ?        S    00:08   0:00  \_ /usr/sbin/httpd
apache    1050  0.0  1.0 185448  5300 ?        S    00:08   0:00  \_ /usr/sbin/httpd
apache    1051  0.0  1.0 185448  5300 ?        S    00:08   0:00  \_ /usr/sbin/httpd
apache    1052  0.0  1.0 185448  5300 ?        S    00:08   0:00  \_ /usr/sbin/httpd
apache    1053  0.0  1.0 185448  5300 ?        S    00:08   0:00  \_ /usr/sbin/httpd
apache    1054  0.0  1.0 185448  5300 ?        S    00:08   0:00  \_ /usr/sbin/httpd
apache    1055  0.0  1.0 185448  5300 ?        S    00:08   0:00  \_ /usr/sbin/httpd
(abbr)


TIPS

Memory Order

ps auwwx --sort=-rss
ps -C httpd -o pid,pmem,sz,rss,vsz,comm --sort -rss
ps auxw -L --sort=-vsz > /tmp/ps.`date +%Y%m%d%k%M%S`

CPU Order

ps auwwx --sort=-pcpu

Check Process number

ps -C httpd | wc -l
ps auxw | grep mysql | grep -v grep | wc -l

Thread number

ps -C httpd -L |wc -l      ←  -L with thred




Output Explanation

ps aux

# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.3  0.0  10348   688 ?        Ss   00:01   0:00 init [3]
root         2  0.0  0.0      0     0 ?        S<   00:01   0:00 [migration/0]
Article Explanation
USER
PID
%CPU cpu utilization of the process
%MEM ratio of the process's resident set size to the physical memory on the machine, expressed as a percentage.
VSZ virtual memory size of the process in KiB
When the process is started, the memory size to be secured.
RSS resident set size, the non-swapped physical memory that a task has used (in kiloBytes).
In the VSZ, size that occupied the physical memory actually
If the swap-out has occurred, I will check whether there is a process size of RSS is extremely large.
TTY
STAT
START
TIME
COMMAND