Question on -o option of Linux's ps command

Version: RHEL 7.9

I ran ps command with -o option like below. But, it just listed 2 processes :
The bash process with PID 4009
The ps command itself with PID 119893. Output also reveals that ps command's parent process is bash process with PID 4009

Why only bash and ps command listed in this output ? But a regular "ps -ef" lists 663 processes.

johndoe@myserver:~ $ ps -o pid,ppid,args
   PID   PPID COMMAND
  4009   4008 -bash
119893   4009 ps -o pid,ppid,args
johndoe@myserver:~ $
johndoe@myserver:~ $
johndoe@myserver:~ $
johndoe@myserver:~ $ ps -ef | wc -l
663

johndoe@myserver:~ $ ps -ef | head -10
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 Mar26 ?        06:08:23 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root          2      0  0 Mar26 ?        00:00:26 [kthreadd]
root          6      2  0 Mar26 ?        00:15:37 [ksoftirqd/0]
root          7      2  0 Mar26 ?        00:02:32 [migration/0]
root          8      2  0 Mar26 ?        00:00:00 [rcu_bh]
root          9      2  0 Mar26 ?        00:47:13 [rcu_sched]
root         10      2  0 Mar26 ?        00:00:00 [lru-add-drain]
root         11      2  0 Mar26 ?        00:00:36 [watchdog/0]
root         12      2  0 Mar26 ?        00:00:30 [watchdog/1]
johndoe@myserver:~ $
johndoe@myserver:~ $ uname -a
Linux myserver.domain.net 3.10.0-1160.76.1.el7.x86_64 #1 SMP Tue Jul 26 14:15:37 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
johndoe@myserver:~ $

Same behaviour in Fedora 38 which has much newer kernel.

Following is what the manpage of Linux's ps command says about -o option. It does not seem to mention that the output will be restricted only for few processes.

-o format
              User-defined format.  format is a single argument in the form of a blank-separated or comma-separated list, which offers a way to specify individual output columns.
              The recognized keywords are described in the STANDARD FORMAT SPECIFIERS section below.  Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as desired.
              If all column headers are empty (ps -o pid= -o comm=) then the header line will not be output.  Column width will increase as needed for wide headers; this may be
              used to widen up columns such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o comm).  Explicit width control (ps opid,wchan:42,cmd) is offered too.  The behavior of ps
              -o pid=X,comm=Y varies with personality; output may be one column named "X,comm=Y" or two columns named "X" and "Y".  Use multiple -o options when in doubt.  Use the
              PS_FORMAT environment variable to specify a default as desired; DefSysV and DefBSD are macros that may be used to choose the default UNIX or BSD columns.

you may want to (re)read the ps manpage, especially the DESCRIPTION at the top of the output.

DESCRIPTION
The ps utility displays a header line, followed by lines containing information about all of your processes that have controlling terminals.

To see every process on the system using standard syntax:
          ps -e
          ps -ef

...

-e     Select all processes.  Identical to -A.

-f     Do full-format listing. This option can be combined with
              many other UNIX-style options to add additional columns.
              It also causes the command arguments to be printed.  When
              used with -L, the NLWP (number of threads) and LWP (thread
              ID) columns will be added.  See the c option, the format
              keyword args, and the format keyword comm.
1 Like

Thanks MunkeHoller. But, any idea why the following ps command lists only 2 processes as described above ?

grid@s53lxmp:~ $ ps -o pid,ppid,args
   PID   PPID COMMAND
 29734  29733 -bash
 57280  29734 ps -o pid,ppid,args
grid@s53lxmp:~ $

Because ps shows processes of the user, ps -e shows all processes

2 Likes

ps -o ...
is a format, like
ps -f
They only list the processes of the current user and current terminal.

ps -e
lists all processes.

Combined with a format option:
ps -eo ...
ps -ef

1 Like

I think the explanation is pretty clear - if not, please let us know what is not , tks

1 Like