How can I logically AND two selection conditions in ps?
up vote
0
down vote
favorite
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.
ps
add a comment |
up vote
0
down vote
favorite
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.
ps
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
ps
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.
ps
ps
edited Dec 4 at 17:08
asked Dec 4 at 16:54
Tim
25.3k72243446
25.3k72243446
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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 individualpgrep
's options for selecting processes also work without change forps
?
– Tim
Dec 4 at 20:24
Yes, individually, at least for-g
,-G
,-s
,-t
,-u
, and-U
.
– Stephen Kitt
Dec 4 at 22:29
I guessps
andpgrep
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 at 22:37
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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 individualpgrep
's options for selecting processes also work without change forps
?
– Tim
Dec 4 at 20:24
Yes, individually, at least for-g
,-G
,-s
,-t
,-u
, and-U
.
– Stephen Kitt
Dec 4 at 22:29
I guessps
andpgrep
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 at 22:37
add a comment |
up vote
2
down vote
accepted
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 individualpgrep
's options for selecting processes also work without change forps
?
– Tim
Dec 4 at 20:24
Yes, individually, at least for-g
,-G
,-s
,-t
,-u
, and-U
.
– Stephen Kitt
Dec 4 at 22:29
I guessps
andpgrep
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 at 22:37
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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
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
edited Dec 4 at 17:20
answered Dec 4 at 16:56
Stephen Kitt
160k24357432
160k24357432
Thanks. Do the individualpgrep
's options for selecting processes also work without change forps
?
– Tim
Dec 4 at 20:24
Yes, individually, at least for-g
,-G
,-s
,-t
,-u
, and-U
.
– Stephen Kitt
Dec 4 at 22:29
I guessps
andpgrep
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 at 22:37
add a comment |
Thanks. Do the individualpgrep
's options for selecting processes also work without change forps
?
– Tim
Dec 4 at 20:24
Yes, individually, at least for-g
,-G
,-s
,-t
,-u
, and-U
.
– Stephen Kitt
Dec 4 at 22:29
I guessps
andpgrep
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 at 22:37
Thanks. Do the individual
pgrep
's options for selecting processes also work without change for ps
?– Tim
Dec 4 at 20:24
Thanks. Do the individual
pgrep
's options for selecting processes also work without change for ps
?– Tim
Dec 4 at 20:24
Yes, individually, at least for
-g
, -G
, -s
, -t
, -u
, and -U
.– Stephen Kitt
Dec 4 at 22:29
Yes, individually, at least for
-g
, -G
, -s
, -t
, -u
, and -U
.– Stephen Kitt
Dec 4 at 22:29
I guess
ps
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 at 22:37
I guess
ps
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 at 22:37
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485950%2fhow-can-i-logically-and-two-selection-conditions-in-ps%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown