How to count the number of instances of a certain process by canonical path (with arguments)?












2















Let's say I run the following commands:



sleep 500
/bin/sleep 500
sleep 30


What I'm interested is, how to count the number of instances of the sleep program, with certain arguments (in this case the only argument is 500).



So in the example above, if I count the number of instances of /bin/sleep 500, it should return 2.



I tried this: pgrep -xfc '/bin/sleep 500', but since it exactly matched the argument in the parenthesis, sleep 500 isn't counted.










share|improve this question

























  • Are you running a system with a /proc filesystem? That makes it easier to find the pathname of each process's executable.

    – Mark Plotnick
    May 25 '16 at 18:13











  • @MarkPlotnick Yes, I am

    – mythic
    May 25 '16 at 18:49
















2















Let's say I run the following commands:



sleep 500
/bin/sleep 500
sleep 30


What I'm interested is, how to count the number of instances of the sleep program, with certain arguments (in this case the only argument is 500).



So in the example above, if I count the number of instances of /bin/sleep 500, it should return 2.



I tried this: pgrep -xfc '/bin/sleep 500', but since it exactly matched the argument in the parenthesis, sleep 500 isn't counted.










share|improve this question

























  • Are you running a system with a /proc filesystem? That makes it easier to find the pathname of each process's executable.

    – Mark Plotnick
    May 25 '16 at 18:13











  • @MarkPlotnick Yes, I am

    – mythic
    May 25 '16 at 18:49














2












2








2








Let's say I run the following commands:



sleep 500
/bin/sleep 500
sleep 30


What I'm interested is, how to count the number of instances of the sleep program, with certain arguments (in this case the only argument is 500).



So in the example above, if I count the number of instances of /bin/sleep 500, it should return 2.



I tried this: pgrep -xfc '/bin/sleep 500', but since it exactly matched the argument in the parenthesis, sleep 500 isn't counted.










share|improve this question
















Let's say I run the following commands:



sleep 500
/bin/sleep 500
sleep 30


What I'm interested is, how to count the number of instances of the sleep program, with certain arguments (in this case the only argument is 500).



So in the example above, if I count the number of instances of /bin/sleep 500, it should return 2.



I tried this: pgrep -xfc '/bin/sleep 500', but since it exactly matched the argument in the parenthesis, sleep 500 isn't counted.







ps






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 8 mins ago









Rui F Ribeiro

40.1k1479136




40.1k1479136










asked May 25 '16 at 16:29









mythicmythic

1435




1435













  • Are you running a system with a /proc filesystem? That makes it easier to find the pathname of each process's executable.

    – Mark Plotnick
    May 25 '16 at 18:13











  • @MarkPlotnick Yes, I am

    – mythic
    May 25 '16 at 18:49



















  • Are you running a system with a /proc filesystem? That makes it easier to find the pathname of each process's executable.

    – Mark Plotnick
    May 25 '16 at 18:13











  • @MarkPlotnick Yes, I am

    – mythic
    May 25 '16 at 18:49

















Are you running a system with a /proc filesystem? That makes it easier to find the pathname of each process's executable.

– Mark Plotnick
May 25 '16 at 18:13





Are you running a system with a /proc filesystem? That makes it easier to find the pathname of each process's executable.

– Mark Plotnick
May 25 '16 at 18:13













@MarkPlotnick Yes, I am

– mythic
May 25 '16 at 18:49





@MarkPlotnick Yes, I am

– mythic
May 25 '16 at 18:49










3 Answers
3






active

oldest

votes


















3














In your example you can go with:



pgrep -fc 'sleep 500'


It matches both /bin/sleep 500 and sleep 500.



Or if you want to be more precise:



pgrep -fc 'sleep 500$'





share|improve this answer

































    3














    On GNU system:



    $ ps --no-header -C sleep -o args | grep -Ec ' 500( |$)'
    2





    share|improve this answer































      2














      On systems that support a Linux-like /proc:



      #!/bin/sh
      if [ $# != 2 ]
      then
      echo usage: "$0" pathname commandline_regexp
      exit 1
      fi
      cd /proc
      for p in [0-9]*
      do
      exe=$(readlink $p/exe 2>/dev/null)
      if [ "$exe" = "$1" ] &&
      cat $p/cmdline 2>/dev/null | tr '' ' ' | grep -q -- "$2"
      then
      echo match $p
      fi
      done


      example:



      $ sleep 500&
      [3] 18280
      $ sleep 600&
      [4] 18281
      $ ./rpgrep /bin/sleep '.*sleep 500 $'
      match 18280
      $ ./rpgrep /bin/sleep '.*sleep.*00'
      match 18280
      match 18281


      Notes:




      • the 2>/dev/null and the separate cat process are used to cope with the possibility that processes may disappear while the script is running.






      share|improve this answer

























        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',
        autoActivateHeartbeat: false,
        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%2f285453%2fhow-to-count-the-number-of-instances-of-a-certain-process-by-canonical-path-wit%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        In your example you can go with:



        pgrep -fc 'sleep 500'


        It matches both /bin/sleep 500 and sleep 500.



        Or if you want to be more precise:



        pgrep -fc 'sleep 500$'





        share|improve this answer






























          3














          In your example you can go with:



          pgrep -fc 'sleep 500'


          It matches both /bin/sleep 500 and sleep 500.



          Or if you want to be more precise:



          pgrep -fc 'sleep 500$'





          share|improve this answer




























            3












            3








            3







            In your example you can go with:



            pgrep -fc 'sleep 500'


            It matches both /bin/sleep 500 and sleep 500.



            Or if you want to be more precise:



            pgrep -fc 'sleep 500$'





            share|improve this answer















            In your example you can go with:



            pgrep -fc 'sleep 500'


            It matches both /bin/sleep 500 and sleep 500.



            Or if you want to be more precise:



            pgrep -fc 'sleep 500$'






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 25 '16 at 21:56

























            answered May 25 '16 at 21:50









            lgagginilgaggini

            41026




            41026

























                3














                On GNU system:



                $ ps --no-header -C sleep -o args | grep -Ec ' 500( |$)'
                2





                share|improve this answer




























                  3














                  On GNU system:



                  $ ps --no-header -C sleep -o args | grep -Ec ' 500( |$)'
                  2





                  share|improve this answer


























                    3












                    3








                    3







                    On GNU system:



                    $ ps --no-header -C sleep -o args | grep -Ec ' 500( |$)'
                    2





                    share|improve this answer













                    On GNU system:



                    $ ps --no-header -C sleep -o args | grep -Ec ' 500( |$)'
                    2






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 25 '16 at 17:08









                    cuonglmcuonglm

                    104k24203302




                    104k24203302























                        2














                        On systems that support a Linux-like /proc:



                        #!/bin/sh
                        if [ $# != 2 ]
                        then
                        echo usage: "$0" pathname commandline_regexp
                        exit 1
                        fi
                        cd /proc
                        for p in [0-9]*
                        do
                        exe=$(readlink $p/exe 2>/dev/null)
                        if [ "$exe" = "$1" ] &&
                        cat $p/cmdline 2>/dev/null | tr '' ' ' | grep -q -- "$2"
                        then
                        echo match $p
                        fi
                        done


                        example:



                        $ sleep 500&
                        [3] 18280
                        $ sleep 600&
                        [4] 18281
                        $ ./rpgrep /bin/sleep '.*sleep 500 $'
                        match 18280
                        $ ./rpgrep /bin/sleep '.*sleep.*00'
                        match 18280
                        match 18281


                        Notes:




                        • the 2>/dev/null and the separate cat process are used to cope with the possibility that processes may disappear while the script is running.






                        share|improve this answer






























                          2














                          On systems that support a Linux-like /proc:



                          #!/bin/sh
                          if [ $# != 2 ]
                          then
                          echo usage: "$0" pathname commandline_regexp
                          exit 1
                          fi
                          cd /proc
                          for p in [0-9]*
                          do
                          exe=$(readlink $p/exe 2>/dev/null)
                          if [ "$exe" = "$1" ] &&
                          cat $p/cmdline 2>/dev/null | tr '' ' ' | grep -q -- "$2"
                          then
                          echo match $p
                          fi
                          done


                          example:



                          $ sleep 500&
                          [3] 18280
                          $ sleep 600&
                          [4] 18281
                          $ ./rpgrep /bin/sleep '.*sleep 500 $'
                          match 18280
                          $ ./rpgrep /bin/sleep '.*sleep.*00'
                          match 18280
                          match 18281


                          Notes:




                          • the 2>/dev/null and the separate cat process are used to cope with the possibility that processes may disappear while the script is running.






                          share|improve this answer




























                            2












                            2








                            2







                            On systems that support a Linux-like /proc:



                            #!/bin/sh
                            if [ $# != 2 ]
                            then
                            echo usage: "$0" pathname commandline_regexp
                            exit 1
                            fi
                            cd /proc
                            for p in [0-9]*
                            do
                            exe=$(readlink $p/exe 2>/dev/null)
                            if [ "$exe" = "$1" ] &&
                            cat $p/cmdline 2>/dev/null | tr '' ' ' | grep -q -- "$2"
                            then
                            echo match $p
                            fi
                            done


                            example:



                            $ sleep 500&
                            [3] 18280
                            $ sleep 600&
                            [4] 18281
                            $ ./rpgrep /bin/sleep '.*sleep 500 $'
                            match 18280
                            $ ./rpgrep /bin/sleep '.*sleep.*00'
                            match 18280
                            match 18281


                            Notes:




                            • the 2>/dev/null and the separate cat process are used to cope with the possibility that processes may disappear while the script is running.






                            share|improve this answer















                            On systems that support a Linux-like /proc:



                            #!/bin/sh
                            if [ $# != 2 ]
                            then
                            echo usage: "$0" pathname commandline_regexp
                            exit 1
                            fi
                            cd /proc
                            for p in [0-9]*
                            do
                            exe=$(readlink $p/exe 2>/dev/null)
                            if [ "$exe" = "$1" ] &&
                            cat $p/cmdline 2>/dev/null | tr '' ' ' | grep -q -- "$2"
                            then
                            echo match $p
                            fi
                            done


                            example:



                            $ sleep 500&
                            [3] 18280
                            $ sleep 600&
                            [4] 18281
                            $ ./rpgrep /bin/sleep '.*sleep 500 $'
                            match 18280
                            $ ./rpgrep /bin/sleep '.*sleep.*00'
                            match 18280
                            match 18281


                            Notes:




                            • the 2>/dev/null and the separate cat process are used to cope with the possibility that processes may disappear while the script is running.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited May 26 '16 at 14:21

























                            answered May 25 '16 at 19:43









                            Mark PlotnickMark Plotnick

                            18.2k24065




                            18.2k24065






























                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f285453%2fhow-to-count-the-number-of-instances-of-a-certain-process-by-canonical-path-wit%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