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.










share|improve this question




























    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.










    share|improve this question


























      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 4 at 17:08

























      asked Dec 4 at 16:54









      Tim

      25.3k72243446




      25.3k72243446






















          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





          share|improve this answer























          • 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










          • 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 at 22:37













          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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

























          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





          share|improve this answer























          • 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










          • 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 at 22:37

















          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





          share|improve this answer























          • 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










          • 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 at 22:37















          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





          share|improve this answer














          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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 4 at 17:20

























          answered Dec 4 at 16:56









          Stephen Kitt

          160k24357432




          160k24357432












          • 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










          • 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 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










          • Yes, individually, at least for -g, -G, -s, -t, -u, and -U.
            – Stephen Kitt
            Dec 4 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 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 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 at 22:37






          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 at 22:37




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Accessing regular linux commands in Huawei's Dopra Linux

          Can't connect RFCOMM socket: Host is down

          Kernel panic - not syncing: Fatal Exception in Interrupt