3

I am reading the ps man page and there is something I don't understand.

-f    does 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.

I just want to add sid to the output of ps -f

But I was confused by the following output, my understanding is -o should just add sid to the output of -f but looks like -o overwrites -f completely.

I know I can specify all the options I want, like pid,ppid,pgid,sid,user,comm etc etc but is there a way just add one additional column to output of -f ? That is what the man page says, correct ?

[njia@cst-cgxfile01 ~]$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
njia      3397 26106  0 12:23 pts/1    00:00:00 ps -f
njia     26106 26105  0 09:45 pts/1    00:00:00 -bash
[njia@ ~]$ ps -f -o sid
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
  SID
26106
26106
25875
25875
25875
25875
25875
[njia@ ~]$

In addition, ps -ef -o sid will limit the process selection to those owned my me, that was a surprise as well.

terdon
  • 242,166

2 Answers2

3

To answer the first part of your question, there are several flags you can add to -f. They include -l, -j, -m, and -L. Unfortunately -o <format> can't be combined with -f.

Indeed, the best way to get exactly what you want, is to specify exactly what you want, e.g.

ps -e -o pid,ppid,pgid,sid,user,comm

But you can get really close by adding -j to -f, to make ps -efj. This adds both the PGID and SID columns.

Demonstrating without the -e flag to make the output shorter, compare:

$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
myuser     123  4513  0 18:20 pts/26   00:00:00 zsh
myuser     1282  123  0 18:20 pts/26   00:00:00 ps -f

$ ps -fj
UID        PID  PPID  PGID   SID  C STIME TTY          TIME CMD
myuser     123  4513   123   123  0 18:20 pts/26   00:00:00 zsh
myuser     1402  123  1402   123  0 18:20 pts/26   00:00:00 ps -fj

To answer the second part of your question, the reason ps -ef -o sid only shows your own processes, is it switches to BSD mode when it decides your flags weren't POSIX compliant. This is indicated by the message

Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ

So it's equivalent to running ps ef o sid.

In BSD mode, e means it will print the process's environment, and f means "forest". And BSD mode defaults to printing all processes owned by the current user that have any terminal, not just those on the current terminal.

Try changing the -o sid to -o sid,cmd to see the effects of e and f options.

$ ps ef o sid
SID
12345
  567
  567
...

$ ps ef o sid,cmd
  SID CMD
12345 -zsh USER=... LOGNAME=...
  567 zsh PWD=... LANG=...
  567  \_ ps ef o sid,cmd LANG=... PWD=...
...

And compare to ps u to see that the processes shown are the same (I added | wc -l for brevity).

$ ps ef o cmd | wc -l
20
$ ps u | wc -l
20
Mikel
  • 57,299
  • 15
  • 134
  • 153
  • I think you're absolutely right, I don't get this error message on my Debian though. – terdon Feb 11 '14 at 02:19
  • ps -eO sid f worked for me but that is even more wired. There are -f in output format control and f in output modifier. I can do this ps -f f and it gives me all the fields included by ps -f but in ps f format. This becomes more interesting. – Ask and Learn Feb 11 '14 at 02:29
  • ps ef sid,cmd gives me a syntax error, do you mean ps ef o sid,cmd ? – Ask and Learn Feb 11 '14 at 02:32
  • Note that this is not actually how BSD ps worked, and was documented, for the whole lifetime of procps ps. The "BSD mode" nomenclature is an error perpetuated by the procps manual. https://unix.stackexchange.com/a/511530/5132 – JdeBP Jun 16 '20 at 09:57
2

Everything you state is correct, ps will only list processes owned by you unless you tell it otherwise with x or a or similar options. -o sets the format, it is not cumulative. So, to get the output of ps -f with SID added, you will need to specify the whole thing using -o:

$ ps -f 
UID        PID  PPID  C STIME TTY          TIME CMD
terdon   27452 27436  0 02:48 pts/3    00:00:00 /bin/bash
terdon   30622 27452  0 02:51 pts/3    00:00:00 ps -f
$ ps -o user,pid,sid,ppid,c,stime,tty,time,cmd
USER       PID   SID  PPID  C STIME TT           TIME CMD
terdon   27452 27452 27436  0 02:48 pts/3    00:00:00 /bin/bash
terdon   30905 27452 27452  0 02:51 pts/3    00:00:00 ps -o user,pid,sid,ppid,c,stime,tty,time,cmd

To get the same output for all users, use this:

ps -axo user,pid,sid,ppid,c,stime,tty,time,cmd

The closest you can get to adding to as opposed to overwriting the output format is using -O (that's O as in Oliver, not 0):

-O format
    Like -o, but preloaded with some default columns.  
    Identical to -o pid,format,state,tname,time,command or
                 -o pid,format,tname,time,cmd

So, for example:

$ ps -O sid
  PID   SID S TTY          TIME COMMAND
 4879 27452 R pts/3    00:00:00 ps -O sid
27452 27452 S pts/3    00:00:00 /bin/bash
terdon
  • 242,166
  • thanks for the reply, I am still confused by ps -ef -O sid only list processes owned by myself, what happened to -e option. Looks like ps just ignored it. It works if I do this ps -eO sid f. – Ask and Learn Feb 11 '14 at 02:11
  • @AskandLearn the ps man page is one of the most complex I've ever seen. I agree that the -f seems to be eating -e but have no idea why. – terdon Feb 11 '14 at 02:17
  • 1
    The -f isn't eating the -e. The -f and the -o conflict, so ps tries to parse it in BSD mode instead. And in BSD mode -e means something different. – Mikel Feb 11 '14 at 03:15
  • @Mikel yes, I understood when I read your answer, thanks! – terdon Feb 11 '14 at 03:18