Sunday, August 28, 2016

19 Awesome practical ps command examples


ps (process status) command will display information of all active processes. Here i’m going to explain all the possible examples about ps command and their usage, When you read this article you will definitely get more details about ps command.

Syntax of PS :

Syntax :# ps [Option]

Options of ps command.

e : To display all the processes.
f : To display full format listing.
F : To display extra full format
l : To display long listing
y : To change the (-l) option output
x : processes w/o controlling ttys
u : Print the output by UID
a : all w/ tty, including other users
C : To display the output by command name
p : To display the output by PID
j : job control
o : user-defined
ppid : To display the output by ppid

1) List Currently Running Processes only.

ps (process status) command is used to display the current process running on your system. The below example shows PID, TTY, TIME and command details.
# ps -e
  PID TTY          TIME CMD
    1 ?        00:05:05 init
    2 ?        00:01:07 kthreadd
    3 ?        00:00:51 migration/0
    4 ?        00:01:06 ksoftirqd/0
    5 ?        00:00:00 migration/0
 2217 ?        01:20:40 nagios
 4550 ?        00:00:00 mysqld_safe
 4633 ?        00:03:19 mysqld
 4682 ?        00:00:00 pure-ftpd
 4684 ?        00:00:00 pure-authd
 5441 ?        00:00:00 dovecot
 5451 ?        00:00:00 pop3-login
 5452 ?        00:00:00 imap-login
 .
 .
 32263 ?        00:00:38 httpd

2) List Currently Running Processes with details.

ps command is used to display the current process running on your system. The below example shows full format listing.
# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Jun19 ?        00:05:05 /sbin/init
root         2     0  0 Jun19 ?        00:01:07 [kthreadd]
root         3     2  0 Jun19 ?        00:00:51 [migration/0]
root         4     2  0 Jun19 ?        00:01:06 [ksoftirqd/0]
named    26458     1  0 Dec02 ?        00:00:07 /usr/sbin/named -u named
.
.
root     32237     1  0 Dec02 ?        00:00:28 abrt-dump-oops -d /var/spool/abrt -rwx /var/log/messages
root     32263     1  0 Dec07 ?        00:00:38 /usr/local/apache/bin/httpd -k start -DSSL

3) List Currently Running Processes with more details.

ps command is used to display the current process running on your system. The below command is shows more column output compare with above command.
# ps -eF
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root         1     0  0  4839  1004   3 Jun19 ?        00:05:05 /sbin/init
root         2     0  0     0     0   1 Jun19 ?        00:01:07 [kthreadd]
root         3     2  0     0     0   0 Jun19 ?        00:00:51 [migration/0]
root         4     2  0     0     0   0 Jun19 ?        00:01:06 [ksoftirqd/0]
root         5     2  0     0     0   0 Jun19 ?        00:00:00 [migration/0]
root         6     2  0     0     0   0 Jun19 ?        00:00:12 [watchdog/0]
root         7     2  0     0     0   1 Jun19 ?        00:01:08 [migration/1]
root         8     2  0     0     0   1 Jun19 ?        00:00:00 [migration/1]
.
.
root     32237     1  0 27573   888   0 Dec02 ?        00:00:28 abrt-dump-oops -d /var/spool/abrt -rwx /var/log/messages
root     32263     1  0 19608  7804   0 Dec07 ?        00:00:38 /usr/local/apache/bin/httpd -k start -DSSL

4) List Currently Running Processes with more details.

ps command is used to display the current process running on your system. The below command is shows more column output compare with above command.
# ps -ely
S   UID   PID  PPID  C PRI  NI   RSS    SZ WCHAN  TTY          TIME CMD
S     0     1     0  0  80   0  1004  4839 poll_s ?        00:05:05 init
S     0     2     0  0  80   0     0     0 kthrea ?        00:01:07 kthreadd
S     0     3     2  0 -40   -     0     0 migrat ?        00:00:51 migration/0
S     0     4     2  0  80   0     0     0 ksofti ?        00:01:06 ksoftirqd/0
S     0     5     2  0 -40   -     0     0 cpu_st ?        00:00:00 migration/0
S     0     6     2  0 -40   -     0     0 watchd ?        00:00:12 watchdog/0
S     0     7     2  0 -40   -     0     0 migrat ?        00:01:08 migration/1
.
.
S     0 32194     1  0  80   0  1068 28120 poll_s ?        00:00:00 abrtd
S     0 32237     1  0  80   0   888 27573 hrtime ?        00:00:28 abrt-dump-oops
S     0 32263     1  0  80   0  7804 19608 poll_s ?        00:00:38 httpd

5) List Currently Running Processes using BSD syntax.

Use the below command to see the current running process using BSD syntax.
# ps -ax
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     5:05 /sbin/init
    2 ?        S      1:07 [kthreadd]
    3 ?        S      0:51 [migration/0]
    4 ?        S      1:06 [ksoftirqd/0]
    5 ?        S      0:00 [migration/0]
    6 ?        S      0:12 [watchdog/0]
 .
 .
32237 ?        Ss     0:28 abrt-dump-oops -d /var/spool/abrt -rwx /var/log/messages
32263 ?        Ss     0:38 /usr/local/apache/bin/httpd -k start -DSSL

6) List Currently Running Processes using BSD syntax with more option.

Use the below command to see the current running process using BSD syntax with more option.
# ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  19356  1004 ?        Ss   Jun19   5:05 /sbin/init
root         2  0.0  0.0      0     0 ?        S    Jun19   1:07 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    Jun19   0:51 [migration/0]
root         4  0.0  0.0      0     0 ?        S    Jun19   1:06 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S    Jun19   0:00 [migration/0]
.
.
root     32237  0.0  0.0 110292   888 ?        Ss   Dec02   0:28 abrt-dump-oops -d /var/spool/abrt -rwx /var/log/messages
root     32263  0.0  0.0  78432  7804 ?        Ss   Dec07   0:38 /usr/local/apache/bin/httpd -k start -DSSL

7) List the Process using UID

ps command is used to display the current process running on your system. The below example list the process with given UID and binded the username
# ps -f -u sshd
UID        PID  PPID  C STIME TTY          TIME CMD
sshd     17685 17684  0 Jul13 ?        00:00:00 sshd: unknown [net]
sshd     26683 26682  0 Sep08 ?        00:00:00 sshd: root [net]
sshd     31198 31195  0 Sep01 ?        00:00:00 sshd: unknown [net]

8) How to list more then one Process using UID

ps command is used to display the current process running on your system. For morethen one UID, use comma to separate it. The below example list the more then one process with UID and binded the username.
# ps -f -u sshd,mysql
UID        PID  PPID  C STIME TTY          TIME CMD
mysql     4633  4550  0 Dec19 ?        00:03:20 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql --user=mysql --log-error=/var/lib/mysql/server.2daygeek.com
sshd     17685 17684  0 Jul13 ?        00:00:00 sshd: unknown [net]
sshd     26683 26682  0 Sep08 ?        00:00:00 sshd: root [net]
sshd     31198 31195  0 Sep01 ?        00:00:00 sshd: unknown [net]

9) List the Process using commands.

You can list the process using particular command, The below example list the process which was binded with httpd command.
# ps -f -C httpd
UID        PID  PPID  C STIME TTY          TIME CMD
nobody   24822 32263  0 12:07 ?        00:00:00 /usr/local/apache/bin/httpd -k start -DSSL
nobody   24823 32263  0 12:07 ?        00:00:00 /usr/local/apache/bin/httpd -k start -DSSL
nobody   24824 32263  0 12:07 ?        00:00:00 /usr/local/apache/bin/httpd -k start -DSSL
nobody   24825 32263  0 12:07 ?        00:00:00 /usr/local/apache/bin/httpd -k start -DSSL
nobody   24826 32263  0 12:07 ?        00:00:00 /usr/local/apache/bin/httpd -k start -DSSL
nobody   24889 32263  0 12:07 ?        00:00:00 /usr/local/apache/bin/httpd -k start -DSSL
root     32263     1  0 Dec07 ?        00:00:38 /usr/local/apache/bin/httpd -k start -DSSL
root@server57324 [~]#

10) List the Process using PID.

You can list the process using particular PID, The below example list the process which was binded with given PID.
# ps -f  -p 1707,6105
UID        PID  PPID  C STIME TTY          TIME CMD
root      1707  1706  0 Jun19 ?        00:00:00 hald-runner
root      6105     1  0 01:16 ?        00:00:00 tailwatchd

11) List the Process using PPID.

You can list the process using particular PPID, The below example list the process which was binded with given PPID.
# ps -f  --ppid 25354
UID        PID  PPID  C STIME TTY          TIME CMD
root      1356 25354  0 Dec02 ?        00:00:00 /sbin/udevd -d
root      1357 25354  0 Dec02 ?        00:00:00 /sbin/udevd -d

12) List the Process with tree structure.

The below example is list the process with tree format. You can alternatively use the “ps axuf” to show the tree output.
# ps axjf
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
 1 19218 19218 19218 ?           -1 Ss       0   0:00 /usr/sbin/sshd
19218 22968 22968 22968 ?           -1 Ss       0   0:00  \_ sshd: root@pts/0
22968 22976 22976 22976 pts/0    22976 Ss+      0   0:00  |   \_ -bash
19218 23356 23356 23356 ?           -1 Ss       0   0:00  \_ sshd: root@pts/1
23356 23369 23369 23369 pts/1    25557 Ss       0   0:00      \_ -bash
23369 25557 25557 23369 pts/1    25557 R+       0   0:00          \_ ps axjf
    1 22402 22402 18353 ?           -1 S        0   0:07 cpsrvd (SSL) - waiting for c --llu=1387783811 --listen=3,4,5,6,7,8,9,10
    1   712   712   712 ?           -1 Ss       0   0:11 lfd - sleeping
    1  5441  5441  5441 ?           -1 Ss       0   0:00 /usr/sbin/dovecot
 5441  5451  5441  5441 ?           -1 S      497   0:00  \_ dovecot/pop3-login
 5441  5452  5441  5441 ?           -1 S      497   0:00  \_ dovecot/imap-login
 5441  5453  5441  5441 ?           -1 S       97   0:00  \_ dovecot/anvil
 5441  5454  5441  5441 ?           -1 S        0   0:00  \_ dovecot/log

13) Print the elapsed time for the process.

The below examples shows the process elapsed time, Its mean how log the particular process is running without down. Here i’m going to check two process elapsed time. The output shows [Date-hour-second-mins].

PID:1 – It’s init process, the process is up 187 days, 14 hours, 09 mins and 56 seconds.
PID:26510 – It’s crond process, the process is up 22 days, 11 hours, 19 mins and 03 seconds.
# ps -p 1,26510 -o pid,etime=
  PID
    1 187-14:09:56
26510 22-11:19:03
root@server57324 [~]#

14) How to check memory Leak using ps command.

The below examples shows the memory leak on your server, Its mean how much of the memory consumed of the corresponding process.
# ps aux --sort pmem
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  19356  1004 ?        Ss   Jun19   5:06 /sbin/init
root      5519  0.0  0.6 167328 52664 ?        Ss   Dec24   0:19 /usr/local/cpanel/3rdparty/perl/514/bin/spamd -d --allowed-ips=127.0.0.1 --pidfile=/var/run/spamd.pid
root      5571  0.0  0.6 167328 50840 ?        S    Dec24   0:00 spamd child
root      5572  0.0  0.6 167328 50840 ?        S    Dec24   0:00 spamd child
root      6105  0.0  0.1  51500 11032 ?        S    Dec24   0:00 tailwatchd
root      6132  0.0  0.2 112744 16484 ?        S    Dec24   0:00 cpdavd - accepting connections on 2077 and 2078
root     19959  0.0  0.1 152068 13180 ?        Ss   00:00   0:04 lfd - sleeping
root     22402  0.0  0.2  98556 18112 ?        S    Dec19   0:10 cpsrvd (SSL) - waiting for c --llu=1387783811 --listen=3,4,5,6,7,8,9,10
named    26458  0.0  0.3 389448 24788 ?        Ssl  Dec02   0:08 /usr/sbin/named -u named

15) How to check memory Leak of particular PID.

The below examples shows the particular PID memory leak on your server. For this you need to use the above command first and take the PID and do it.
# ps ev --pid=5572
  PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
 5572 ?        S      0:00      0     4 167323 50840  0.6 spamd child

16) To see every process running as root (real & effective ID) in user format.

The below examples shows the root user process.
# ps -U root -u root u
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  19356  1004 ?        Ss   Jun19   5:06 /sbin/init
root     31943  0.0  0.1  16868 12772 ?        SLsl Dec02   2:03 /sbin/mdmon --takeover md0
root     31949  0.0  0.0   4688   580 ?        Ss   Dec02   0:10 mdadm --monitor --scan -f --pid-file=/var/run/mdadm/mdadm.pid
root     32194  0.0  0.0 112480  1068 ?        Ss   Dec02   0:00 /usr/sbin/abrtd
root     32237  0.0  0.0 110292   888 ?        Ss   Dec02   0:30 abrt-dump-oops -d /var/spool/abrt -rwx /var/log/messages
root     32263  0.0  0.0  78536  7824 ?        Ss   Dec07   0:42 /usr/local/apache/bin/httpd -k start -DSSL

17) Customizing the ps output format.

We can print the ps output as a customized report what we want.
# ps -eo uname,pid,ppid,nlwp,pcpu,pmem,psr,start_time,tty,time,args
USER       PID  PPID NLWP %CPU %MEM PSR START TT           TIME COMMAND
root         1     0    1  0.0  0.0   1 Jun19 ?        00:05:06 /sbin/init
root         2     0    1  0.0  0.0   1 Jun19 ?        00:01:07 [kthreadd]
nagios    2217     1    2  0.0  0.0   0 Jun19 ?        01:21:09 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
root     31949     1    1  0.0  0.0   0 Dec02 ?        00:00:10 mdadm --monitor --scan -f --pid-file=/var/run/mdadm/mdadm.pid
root     32194     1    1  0.0  0.0   0 Dec02 ?        00:00:00 /usr/sbin/abrtd
root     32237     1    1  0.0  0.0   3 Dec02 ?        00:00:30 abrt-dump-oops -d /var/spool/abrt -rwx /var/log/messages
root     32263     1    1  0.0  0.0   3 Dec07 ?        00:00:42 /usr/local/apache/bin/httpd -k start -DSSL

18) To display particular user process.

We can print the particular user process using below command.
# ps U actadmin
  PID TTY      STAT   TIME COMMAND
20249 ?        S      0:01 sshd: actadmin@pts/1
20250 pts/1    Ss     0:00 -bash

19) To display current user process.

We can print the current user process using below command.
# ps U $USER
  PID TTY      STAT   TIME COMMAND
 1560 ?        S      0:00 sshd: mageshm@pts/1
 1563 pts/1    Ss     0:00 -bash
 1616 pts/1    R+     0:00 ps U mageshm

Note :

I have tried maximum of ps command possible examples. If you need more details about ps, check the man page.

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete