DEV Community

Ajay Singh
Ajay Singh

Posted on • Updated on

Intro to ps command

The aim of the article is to give an idea about ps command and various option it is used to run.
I have seen people running commands like

ps -ef

without understanding the reason for adding the parameters ( they do because of somewhere they have seen in StackOverflow or seen their colleagues using it while debugging some issue).

To start with what does ps command is used for: displays information about a selection of the active processes.

ps accepts several kinds of options which can be clubbed as below

1 UNIX options, which may be grouped and must be preceded by a dash.
2 BSD options, which may be grouped and must not be used with a dash.
3 GNU long options, which are preceded by two dashes.

ps command works by reading files in the proc filesystem. The directory /proc/PID contains various files that provide information about process PID. The content of these files is generated on the fly by the kernel when a process reads them.

By default, ps selects all processes with the same effective user ID (euid=EUID) as the current user and associated with the same terminal as the invoker.

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.

[ajays@ip-10-40-19-132 ~]$ ps
  PID TTY          TIME CMD
20244 pts/0    00:00:00 bash
20342 pts/0    00:00:00 ps
[ajays@ip-10-40-19-132 ~]$ who
ajays    pts/0        2019-08-06 20:38 (
[ajays@ip-10-40-19-132 ~]$

So as per the explanation, the ps command would have only shown the process with the same user and associated as with the same terminal as invoker. The next command list down the invoker ( ajays ) and terminal ( pts/0 ). So the output of the ps command is the process associated with ajays and attached to terminal pts/0.

The options used by ps can be classified in mainly below categories.

  • Process selection ( simple or by list )
  • Output format control and modifiers
  • Thread display

Each of the above categories has a lot of option which we cannot cover in this article but will try to cover some common ones.

ps -f

The -f option is UNIX option. It is used to do full-format listing.
The out show following details UID, PID, PPID, C, STIME, TTY, TIME, CMD.

UID - The name of the user who have started the process.

PID - This act as the identification no of the process running in the memory.

PPID- It is parent process id. This id is the pid of the process because of
which these process has been started.

C- Processor utilization information in %.

STIME- This is the start time of the process.

TTY- This is the terminal from which the process was started.

TIME- Total time for which the process has utilized cpu.

CMD- The command and arguments executed.

[ajays@ip-10-40-19-132 ~]$ ps -f
ajays    20244 20213  0 20:38 pts/0    00:00:00 -bash
ajays    29906 20244  0 22:12 pts/0    00:00:00 ps -f
[ajays@ip-10-40-19-132 ~]$

ps -ef

Now we have attached one more argument -e (this is a UNIX based option).
This option is a select option. Select all processes.
a snippet of this command looks like below

5984      6011  5341  0 Jul31 ?        00:00:00 /opt/couchdb/bin/couchjs -S 268435456 /opt/couchdb/share/server/ma
5984      6012  5341  0 Jul31 ?        00:00:01 /opt/couchdb/bin/couchjs -S 268435456 /opt/couchdb/share/server/ma
5984      6013  5341  0 Jul31 ?        00:00:00 /opt/couchdb/bin/couchjs -S 268435456 /opt/couchdb/share/server/ma
5984      6014  5341  0 Jul31 ?        00:00:03 /opt/couchdb/bin/couchjs -S 268435456 /opt/couchdb/share/server/ma
5984      6015  5341  0 Jul31 ?        00:00:09 /opt/couchdb/bin/couchjs -S 268435456 /opt/couchdb/share/server/ma
5984      6016  5341  0 Jul31 ?        00:00:07 /opt/couchdb/bin/couchjs -S 268435456 /opt/couchdb/share/server/ma
5984      6017  5341  0 Jul31 ?        00:00:08 /opt/couchdb/bin/couchjs -S 268435456 /opt/couchdb/share/server/ma
ajays    23552 20244  0 23:07 pts/0    00:00:00 ps -ef
postfix  24642  3429  0 22:01 ?        00:00:00 pickup -l -t unix -u

ps -af

Now we have attached -a ( this is UNIX based option ).
Select all processes except both session leaders and processes not associated with a terminal.
So the difference between -a and -e is evident, -e will also return the process which are not associated to a terminal where -a will only return process which are associated with a terminal.

Session Leaders are the process where PID=SID.

Top comments (0)