0

I am simulating the default behavior of ps without arguments:

  • One selection condition is to find processes with controlling terminal being the same as the one of the current shell.

  • The other selection condition is to find processes with euid as the current user.

So I need to logically AND the selections by user and terminal.

$ ps  -u t -t /dev/pts/4 -o pid,tname,time,ucmd   
$ ps  -u t -t pts/4 -o pid,tname,time,ucmd   
$ ps  -u t -t /dev/tty -o pid,tname,time,ucmd 

I know the controlling terminal of the current shell is /dev/pts/4. But all give me processes with other controlling terminals and without controlling terminals. Thanks.

2

Process selection is cumulative with -u and -t: you’re selecting all processes running as user t, and, on top of those, all processes with controlling terminal /dev/pts/4 or /dev/tty. To see the processes with a given controlling terminal, use -t on its own:

ps -t pts/4 -o pid,tname,time,ucmd

As specified by POSIX, process selection options are additive:

With the exception of -f, -l, -n namelist, and -o format, all of the options shown are used to select processes. If any are specified, the default list shall be ignored and ps shall select the processes represented by the inclusive OR of all the selection-criteria options.

To combine criteria, only listing processes which match all of them, you can use pgrep:

ps -p $(pgrep -d, -u t -t pts/4) -o pid,tname,time,ucmd
  • Thanks. Do the individual pgrep's options for selecting processes also work without change for ps? – Tim Dec 4 '18 at 20:24
  • Yes, individually, at least for -g, -G, -s, -t, -u, and -U. – Stephen Kitt Dec 4 '18 at 22:29
  • I guessps and pgrep might be created deliberately to behave opposite to each other, instead of each having both AND and OR features. So we will have to use both together sometimes. I don't know what good that can bring. – Tim Dec 4 '18 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.