Excluding grep from process list











up vote
26
down vote

favorite
13












I have cobbled together a command to return the process ID of a running daemon:



ps aux | grep daemon_name | awk "{ print $2 }"


It works perfectly and returns the PID, but it also returns a second PID which is presumably the process I'm running now. Is there a way I can exclude my command from the list of returned PIDs?



I've tested it a few times and it appears my command is always the second PID in the list, but I don't want to grab just the first PID in case it's inaccurate.










share|improve this question






















  • serverfault.com/questions/367921/…
    – Der Hochstapler
    Apr 7 '12 at 12:29















up vote
26
down vote

favorite
13












I have cobbled together a command to return the process ID of a running daemon:



ps aux | grep daemon_name | awk "{ print $2 }"


It works perfectly and returns the PID, but it also returns a second PID which is presumably the process I'm running now. Is there a way I can exclude my command from the list of returned PIDs?



I've tested it a few times and it appears my command is always the second PID in the list, but I don't want to grab just the first PID in case it's inaccurate.










share|improve this question






















  • serverfault.com/questions/367921/…
    – Der Hochstapler
    Apr 7 '12 at 12:29













up vote
26
down vote

favorite
13









up vote
26
down vote

favorite
13






13





I have cobbled together a command to return the process ID of a running daemon:



ps aux | grep daemon_name | awk "{ print $2 }"


It works perfectly and returns the PID, but it also returns a second PID which is presumably the process I'm running now. Is there a way I can exclude my command from the list of returned PIDs?



I've tested it a few times and it appears my command is always the second PID in the list, but I don't want to grab just the first PID in case it's inaccurate.










share|improve this question













I have cobbled together a command to return the process ID of a running daemon:



ps aux | grep daemon_name | awk "{ print $2 }"


It works perfectly and returns the PID, but it also returns a second PID which is presumably the process I'm running now. Is there a way I can exclude my command from the list of returned PIDs?



I've tested it a few times and it appears my command is always the second PID in the list, but I don't want to grab just the first PID in case it's inaccurate.







grep ps pid






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 7 '12 at 12:12









tak

133124




133124












  • serverfault.com/questions/367921/…
    – Der Hochstapler
    Apr 7 '12 at 12:29


















  • serverfault.com/questions/367921/…
    – Der Hochstapler
    Apr 7 '12 at 12:29
















serverfault.com/questions/367921/…
– Der Hochstapler
Apr 7 '12 at 12:29




serverfault.com/questions/367921/…
– Der Hochstapler
Apr 7 '12 at 12:29










7 Answers
7






active

oldest

votes

















up vote
26
down vote



accepted










grep's -v switch reverses the result, excluding it from the queue. So make it like:



ps aux | grep daemon_name | grep -v "grep daemon_name" | awk "{ print $2 }"


Upd. You can also use -C switch to specify command name like so:



ps -C daemon_name -o pid=


The latter -o determines which columns of the information you want in the listing. pid lists only the process id column. And the equal sign = after pid means there will be no column title for that one, so you get only the clear numbers - PID's.



Hope this helps.






share|improve this answer



















  • 2




    Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
    – phil_w
    Apr 9 '15 at 15:01












  • Good point, @phil_w, thanks. Updated the answer.
    – Serg ikS
    Apr 10 '15 at 7:50




















up vote
23
down vote













You can use a character class trick. "[d]" does not match "[d]" only "d".



 ps aux | grep [d]aemon_name | awk "{ print $2 }"


I prefer this to using | grep -v grep.






share|improve this answer





















  • Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and | grep -v grep will be easier to understand when I revisit my code.
    – tak
    Apr 7 '12 at 12:29










  • I am trying to understand how this [h]ack works. Could you please explain what you mean with "[d]" does not match "[d]" only "d"?
    – Nowaker
    Sep 28 '13 at 21:13










  • @Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so the grep command won't match the ps output line for the grep command itself (what tak called "a second PID" in the original question). expression a[xy]d does not match text a[xy]d but it does match text axd or text ayd. Hence expression [f]oo matches text foo but not text [f]oo. I hope that is clear.
    – RedGrittyBrick
    Sep 28 '13 at 21:34












  • Okay, I was wrong about ARGV. This is just a regexp. Thanks.
    – Nowaker
    Sep 29 '13 at 1:29










  • Devilishly clever! Had to think about it for a moment -- thanks!
    – Subfuzion
    Oct 23 '13 at 18:46


















up vote
15
down vote













Avoid parsing ps's output if there are more reliable alternatives.



pgrep daemon_name
pidof daemon_name





share|improve this answer





















  • Yay, finally process management makes sense now.
    – Błażej Michalik
    Jun 2 '16 at 6:09










  • pgrep searches by substring while pidof by exact match - exaclty what I needed! thanks!
    – Sasha
    Aug 19 '16 at 11:14




















up vote
3
down vote













The ps -C option is not universal on all Unix based systems but if it works on your systems. Instead I would avoid grep altogether:



ps aux | awk '/daemon_name/ && !/awk/ { print $2 }'



No need to escape anything in single quotation marks. ps aux will give you the full list of processes on most Unix based systems and awk is typically installed by default.






share|improve this answer




























    up vote
    3
    down vote













    Use pgrep to look for the pid of a process by name:



    pgrep proc_name


    With extra process name in the result (-l):



    pgrep -l proc_name


    Look for and display the process name (-l) and arguments (-f):



    pgrep -lf proc_name_or_argument


    The good thing about pgrep is that it will never report itself as a match. But you don't need to get the pid by pgrep and then kill the corresponding process by kill. Use pkill instead:



    pkill proc_name


    Specify the SIGKILL signal (-9 or -KILL) instead of SIGTERM (by default):



    pkill -9 proc_name


    Look for the process name (-l) and arguments (-f), ask for confirmation (-I) before killing it by SIGKILL signal (-9 or -KILL):



    pkill -KILL -Ilf proc_name_or_argument


    Notice that the -I option is only available on some versions of pkill, e.g. the one on the OS X Mavericks.






    share|improve this answer






























      up vote
      0
      down vote













      If you are using bash, you can also do this in the following manner by making use of ps -eaf



      PIDS=$(ps -eaf)
      PID=$(echo "$PIDS" | grep "process_name" | awk '{print $2}')
      echo $PID





      share|improve this answer





















      • This Is needlessly complex. Why not simply call ps -eaf | grep "process_name" | awk '{print $2}'
        – David Nedrow
        Jul 4 at 13:25




















      up vote
      0
      down vote













      this line gives you back the pid (process id) excluding "grep"



      PID=$(ps aux | grep '/usr/bin/python2.7 manage.py SES__boto3_sqs_read' | grep -v grep)





      share|improve this answer























      • Some explanation would be nice.
        – davidbaumann
        Nov 19 at 15:03










      • This is basically the same as the accepted answer, but with different decoration.
        – Scott
        Nov 19 at 20:29











      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fsuperuser.com%2fquestions%2f409655%2fexcluding-grep-from-process-list%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      7 Answers
      7






      active

      oldest

      votes








      7 Answers
      7






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      26
      down vote



      accepted










      grep's -v switch reverses the result, excluding it from the queue. So make it like:



      ps aux | grep daemon_name | grep -v "grep daemon_name" | awk "{ print $2 }"


      Upd. You can also use -C switch to specify command name like so:



      ps -C daemon_name -o pid=


      The latter -o determines which columns of the information you want in the listing. pid lists only the process id column. And the equal sign = after pid means there will be no column title for that one, so you get only the clear numbers - PID's.



      Hope this helps.






      share|improve this answer



















      • 2




        Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
        – phil_w
        Apr 9 '15 at 15:01












      • Good point, @phil_w, thanks. Updated the answer.
        – Serg ikS
        Apr 10 '15 at 7:50

















      up vote
      26
      down vote



      accepted










      grep's -v switch reverses the result, excluding it from the queue. So make it like:



      ps aux | grep daemon_name | grep -v "grep daemon_name" | awk "{ print $2 }"


      Upd. You can also use -C switch to specify command name like so:



      ps -C daemon_name -o pid=


      The latter -o determines which columns of the information you want in the listing. pid lists only the process id column. And the equal sign = after pid means there will be no column title for that one, so you get only the clear numbers - PID's.



      Hope this helps.






      share|improve this answer



















      • 2




        Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
        – phil_w
        Apr 9 '15 at 15:01












      • Good point, @phil_w, thanks. Updated the answer.
        – Serg ikS
        Apr 10 '15 at 7:50















      up vote
      26
      down vote



      accepted







      up vote
      26
      down vote



      accepted






      grep's -v switch reverses the result, excluding it from the queue. So make it like:



      ps aux | grep daemon_name | grep -v "grep daemon_name" | awk "{ print $2 }"


      Upd. You can also use -C switch to specify command name like so:



      ps -C daemon_name -o pid=


      The latter -o determines which columns of the information you want in the listing. pid lists only the process id column. And the equal sign = after pid means there will be no column title for that one, so you get only the clear numbers - PID's.



      Hope this helps.






      share|improve this answer














      grep's -v switch reverses the result, excluding it from the queue. So make it like:



      ps aux | grep daemon_name | grep -v "grep daemon_name" | awk "{ print $2 }"


      Upd. You can also use -C switch to specify command name like so:



      ps -C daemon_name -o pid=


      The latter -o determines which columns of the information you want in the listing. pid lists only the process id column. And the equal sign = after pid means there will be no column title for that one, so you get only the clear numbers - PID's.



      Hope this helps.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 10 '15 at 7:52

























      answered Apr 7 '12 at 12:19









      Serg ikS

      37635




      37635








      • 2




        Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
        – phil_w
        Apr 9 '15 at 15:01












      • Good point, @phil_w, thanks. Updated the answer.
        – Serg ikS
        Apr 10 '15 at 7:50
















      • 2




        Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
        – phil_w
        Apr 9 '15 at 15:01












      • Good point, @phil_w, thanks. Updated the answer.
        – Serg ikS
        Apr 10 '15 at 7:50










      2




      2




      Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
      – phil_w
      Apr 9 '15 at 15:01






      Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
      – phil_w
      Apr 9 '15 at 15:01














      Good point, @phil_w, thanks. Updated the answer.
      – Serg ikS
      Apr 10 '15 at 7:50






      Good point, @phil_w, thanks. Updated the answer.
      – Serg ikS
      Apr 10 '15 at 7:50














      up vote
      23
      down vote













      You can use a character class trick. "[d]" does not match "[d]" only "d".



       ps aux | grep [d]aemon_name | awk "{ print $2 }"


      I prefer this to using | grep -v grep.






      share|improve this answer





















      • Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and | grep -v grep will be easier to understand when I revisit my code.
        – tak
        Apr 7 '12 at 12:29










      • I am trying to understand how this [h]ack works. Could you please explain what you mean with "[d]" does not match "[d]" only "d"?
        – Nowaker
        Sep 28 '13 at 21:13










      • @Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so the grep command won't match the ps output line for the grep command itself (what tak called "a second PID" in the original question). expression a[xy]d does not match text a[xy]d but it does match text axd or text ayd. Hence expression [f]oo matches text foo but not text [f]oo. I hope that is clear.
        – RedGrittyBrick
        Sep 28 '13 at 21:34












      • Okay, I was wrong about ARGV. This is just a regexp. Thanks.
        – Nowaker
        Sep 29 '13 at 1:29










      • Devilishly clever! Had to think about it for a moment -- thanks!
        – Subfuzion
        Oct 23 '13 at 18:46















      up vote
      23
      down vote













      You can use a character class trick. "[d]" does not match "[d]" only "d".



       ps aux | grep [d]aemon_name | awk "{ print $2 }"


      I prefer this to using | grep -v grep.






      share|improve this answer





















      • Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and | grep -v grep will be easier to understand when I revisit my code.
        – tak
        Apr 7 '12 at 12:29










      • I am trying to understand how this [h]ack works. Could you please explain what you mean with "[d]" does not match "[d]" only "d"?
        – Nowaker
        Sep 28 '13 at 21:13










      • @Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so the grep command won't match the ps output line for the grep command itself (what tak called "a second PID" in the original question). expression a[xy]d does not match text a[xy]d but it does match text axd or text ayd. Hence expression [f]oo matches text foo but not text [f]oo. I hope that is clear.
        – RedGrittyBrick
        Sep 28 '13 at 21:34












      • Okay, I was wrong about ARGV. This is just a regexp. Thanks.
        – Nowaker
        Sep 29 '13 at 1:29










      • Devilishly clever! Had to think about it for a moment -- thanks!
        – Subfuzion
        Oct 23 '13 at 18:46













      up vote
      23
      down vote










      up vote
      23
      down vote









      You can use a character class trick. "[d]" does not match "[d]" only "d".



       ps aux | grep [d]aemon_name | awk "{ print $2 }"


      I prefer this to using | grep -v grep.






      share|improve this answer












      You can use a character class trick. "[d]" does not match "[d]" only "d".



       ps aux | grep [d]aemon_name | awk "{ print $2 }"


      I prefer this to using | grep -v grep.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 7 '12 at 12:26









      RedGrittyBrick

      66.3k12104160




      66.3k12104160












      • Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and | grep -v grep will be easier to understand when I revisit my code.
        – tak
        Apr 7 '12 at 12:29










      • I am trying to understand how this [h]ack works. Could you please explain what you mean with "[d]" does not match "[d]" only "d"?
        – Nowaker
        Sep 28 '13 at 21:13










      • @Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so the grep command won't match the ps output line for the grep command itself (what tak called "a second PID" in the original question). expression a[xy]d does not match text a[xy]d but it does match text axd or text ayd. Hence expression [f]oo matches text foo but not text [f]oo. I hope that is clear.
        – RedGrittyBrick
        Sep 28 '13 at 21:34












      • Okay, I was wrong about ARGV. This is just a regexp. Thanks.
        – Nowaker
        Sep 29 '13 at 1:29










      • Devilishly clever! Had to think about it for a moment -- thanks!
        – Subfuzion
        Oct 23 '13 at 18:46


















      • Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and | grep -v grep will be easier to understand when I revisit my code.
        – tak
        Apr 7 '12 at 12:29










      • I am trying to understand how this [h]ack works. Could you please explain what you mean with "[d]" does not match "[d]" only "d"?
        – Nowaker
        Sep 28 '13 at 21:13










      • @Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so the grep command won't match the ps output line for the grep command itself (what tak called "a second PID" in the original question). expression a[xy]d does not match text a[xy]d but it does match text axd or text ayd. Hence expression [f]oo matches text foo but not text [f]oo. I hope that is clear.
        – RedGrittyBrick
        Sep 28 '13 at 21:34












      • Okay, I was wrong about ARGV. This is just a regexp. Thanks.
        – Nowaker
        Sep 29 '13 at 1:29










      • Devilishly clever! Had to think about it for a moment -- thanks!
        – Subfuzion
        Oct 23 '13 at 18:46
















      Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and | grep -v grep will be easier to understand when I revisit my code.
      – tak
      Apr 7 '12 at 12:29




      Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and | grep -v grep will be easier to understand when I revisit my code.
      – tak
      Apr 7 '12 at 12:29












      I am trying to understand how this [h]ack works. Could you please explain what you mean with "[d]" does not match "[d]" only "d"?
      – Nowaker
      Sep 28 '13 at 21:13




      I am trying to understand how this [h]ack works. Could you please explain what you mean with "[d]" does not match "[d]" only "d"?
      – Nowaker
      Sep 28 '13 at 21:13












      @Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so the grep command won't match the ps output line for the grep command itself (what tak called "a second PID" in the original question). expression a[xy]d does not match text a[xy]d but it does match text axd or text ayd. Hence expression [f]oo matches text foo but not text [f]oo. I hope that is clear.
      – RedGrittyBrick
      Sep 28 '13 at 21:34






      @Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so the grep command won't match the ps output line for the grep command itself (what tak called "a second PID" in the original question). expression a[xy]d does not match text a[xy]d but it does match text axd or text ayd. Hence expression [f]oo matches text foo but not text [f]oo. I hope that is clear.
      – RedGrittyBrick
      Sep 28 '13 at 21:34














      Okay, I was wrong about ARGV. This is just a regexp. Thanks.
      – Nowaker
      Sep 29 '13 at 1:29




      Okay, I was wrong about ARGV. This is just a regexp. Thanks.
      – Nowaker
      Sep 29 '13 at 1:29












      Devilishly clever! Had to think about it for a moment -- thanks!
      – Subfuzion
      Oct 23 '13 at 18:46




      Devilishly clever! Had to think about it for a moment -- thanks!
      – Subfuzion
      Oct 23 '13 at 18:46










      up vote
      15
      down vote













      Avoid parsing ps's output if there are more reliable alternatives.



      pgrep daemon_name
      pidof daemon_name





      share|improve this answer





















      • Yay, finally process management makes sense now.
        – Błażej Michalik
        Jun 2 '16 at 6:09










      • pgrep searches by substring while pidof by exact match - exaclty what I needed! thanks!
        – Sasha
        Aug 19 '16 at 11:14

















      up vote
      15
      down vote













      Avoid parsing ps's output if there are more reliable alternatives.



      pgrep daemon_name
      pidof daemon_name





      share|improve this answer





















      • Yay, finally process management makes sense now.
        – Błażej Michalik
        Jun 2 '16 at 6:09










      • pgrep searches by substring while pidof by exact match - exaclty what I needed! thanks!
        – Sasha
        Aug 19 '16 at 11:14















      up vote
      15
      down vote










      up vote
      15
      down vote









      Avoid parsing ps's output if there are more reliable alternatives.



      pgrep daemon_name
      pidof daemon_name





      share|improve this answer












      Avoid parsing ps's output if there are more reliable alternatives.



      pgrep daemon_name
      pidof daemon_name






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 7 '12 at 12:29









      grawity

      229k35481540




      229k35481540












      • Yay, finally process management makes sense now.
        – Błażej Michalik
        Jun 2 '16 at 6:09










      • pgrep searches by substring while pidof by exact match - exaclty what I needed! thanks!
        – Sasha
        Aug 19 '16 at 11:14




















      • Yay, finally process management makes sense now.
        – Błażej Michalik
        Jun 2 '16 at 6:09










      • pgrep searches by substring while pidof by exact match - exaclty what I needed! thanks!
        – Sasha
        Aug 19 '16 at 11:14


















      Yay, finally process management makes sense now.
      – Błażej Michalik
      Jun 2 '16 at 6:09




      Yay, finally process management makes sense now.
      – Błażej Michalik
      Jun 2 '16 at 6:09












      pgrep searches by substring while pidof by exact match - exaclty what I needed! thanks!
      – Sasha
      Aug 19 '16 at 11:14






      pgrep searches by substring while pidof by exact match - exaclty what I needed! thanks!
      – Sasha
      Aug 19 '16 at 11:14












      up vote
      3
      down vote













      The ps -C option is not universal on all Unix based systems but if it works on your systems. Instead I would avoid grep altogether:



      ps aux | awk '/daemon_name/ && !/awk/ { print $2 }'



      No need to escape anything in single quotation marks. ps aux will give you the full list of processes on most Unix based systems and awk is typically installed by default.






      share|improve this answer

























        up vote
        3
        down vote













        The ps -C option is not universal on all Unix based systems but if it works on your systems. Instead I would avoid grep altogether:



        ps aux | awk '/daemon_name/ && !/awk/ { print $2 }'



        No need to escape anything in single quotation marks. ps aux will give you the full list of processes on most Unix based systems and awk is typically installed by default.






        share|improve this answer























          up vote
          3
          down vote










          up vote
          3
          down vote









          The ps -C option is not universal on all Unix based systems but if it works on your systems. Instead I would avoid grep altogether:



          ps aux | awk '/daemon_name/ && !/awk/ { print $2 }'



          No need to escape anything in single quotation marks. ps aux will give you the full list of processes on most Unix based systems and awk is typically installed by default.






          share|improve this answer












          The ps -C option is not universal on all Unix based systems but if it works on your systems. Instead I would avoid grep altogether:



          ps aux | awk '/daemon_name/ && !/awk/ { print $2 }'



          No need to escape anything in single quotation marks. ps aux will give you the full list of processes on most Unix based systems and awk is typically installed by default.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 29 '13 at 2:52









          MOG73

          311




          311






















              up vote
              3
              down vote













              Use pgrep to look for the pid of a process by name:



              pgrep proc_name


              With extra process name in the result (-l):



              pgrep -l proc_name


              Look for and display the process name (-l) and arguments (-f):



              pgrep -lf proc_name_or_argument


              The good thing about pgrep is that it will never report itself as a match. But you don't need to get the pid by pgrep and then kill the corresponding process by kill. Use pkill instead:



              pkill proc_name


              Specify the SIGKILL signal (-9 or -KILL) instead of SIGTERM (by default):



              pkill -9 proc_name


              Look for the process name (-l) and arguments (-f), ask for confirmation (-I) before killing it by SIGKILL signal (-9 or -KILL):



              pkill -KILL -Ilf proc_name_or_argument


              Notice that the -I option is only available on some versions of pkill, e.g. the one on the OS X Mavericks.






              share|improve this answer



























                up vote
                3
                down vote













                Use pgrep to look for the pid of a process by name:



                pgrep proc_name


                With extra process name in the result (-l):



                pgrep -l proc_name


                Look for and display the process name (-l) and arguments (-f):



                pgrep -lf proc_name_or_argument


                The good thing about pgrep is that it will never report itself as a match. But you don't need to get the pid by pgrep and then kill the corresponding process by kill. Use pkill instead:



                pkill proc_name


                Specify the SIGKILL signal (-9 or -KILL) instead of SIGTERM (by default):



                pkill -9 proc_name


                Look for the process name (-l) and arguments (-f), ask for confirmation (-I) before killing it by SIGKILL signal (-9 or -KILL):



                pkill -KILL -Ilf proc_name_or_argument


                Notice that the -I option is only available on some versions of pkill, e.g. the one on the OS X Mavericks.






                share|improve this answer

























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  Use pgrep to look for the pid of a process by name:



                  pgrep proc_name


                  With extra process name in the result (-l):



                  pgrep -l proc_name


                  Look for and display the process name (-l) and arguments (-f):



                  pgrep -lf proc_name_or_argument


                  The good thing about pgrep is that it will never report itself as a match. But you don't need to get the pid by pgrep and then kill the corresponding process by kill. Use pkill instead:



                  pkill proc_name


                  Specify the SIGKILL signal (-9 or -KILL) instead of SIGTERM (by default):



                  pkill -9 proc_name


                  Look for the process name (-l) and arguments (-f), ask for confirmation (-I) before killing it by SIGKILL signal (-9 or -KILL):



                  pkill -KILL -Ilf proc_name_or_argument


                  Notice that the -I option is only available on some versions of pkill, e.g. the one on the OS X Mavericks.






                  share|improve this answer














                  Use pgrep to look for the pid of a process by name:



                  pgrep proc_name


                  With extra process name in the result (-l):



                  pgrep -l proc_name


                  Look for and display the process name (-l) and arguments (-f):



                  pgrep -lf proc_name_or_argument


                  The good thing about pgrep is that it will never report itself as a match. But you don't need to get the pid by pgrep and then kill the corresponding process by kill. Use pkill instead:



                  pkill proc_name


                  Specify the SIGKILL signal (-9 or -KILL) instead of SIGTERM (by default):



                  pkill -9 proc_name


                  Look for the process name (-l) and arguments (-f), ask for confirmation (-I) before killing it by SIGKILL signal (-9 or -KILL):



                  pkill -KILL -Ilf proc_name_or_argument


                  Notice that the -I option is only available on some versions of pkill, e.g. the one on the OS X Mavericks.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 26 '14 at 1:28

























                  answered Aug 26 '14 at 1:21









                  Rockallite

                  21123




                  21123






















                      up vote
                      0
                      down vote













                      If you are using bash, you can also do this in the following manner by making use of ps -eaf



                      PIDS=$(ps -eaf)
                      PID=$(echo "$PIDS" | grep "process_name" | awk '{print $2}')
                      echo $PID





                      share|improve this answer





















                      • This Is needlessly complex. Why not simply call ps -eaf | grep "process_name" | awk '{print $2}'
                        – David Nedrow
                        Jul 4 at 13:25

















                      up vote
                      0
                      down vote













                      If you are using bash, you can also do this in the following manner by making use of ps -eaf



                      PIDS=$(ps -eaf)
                      PID=$(echo "$PIDS" | grep "process_name" | awk '{print $2}')
                      echo $PID





                      share|improve this answer





















                      • This Is needlessly complex. Why not simply call ps -eaf | grep "process_name" | awk '{print $2}'
                        – David Nedrow
                        Jul 4 at 13:25















                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      If you are using bash, you can also do this in the following manner by making use of ps -eaf



                      PIDS=$(ps -eaf)
                      PID=$(echo "$PIDS" | grep "process_name" | awk '{print $2}')
                      echo $PID





                      share|improve this answer












                      If you are using bash, you can also do this in the following manner by making use of ps -eaf



                      PIDS=$(ps -eaf)
                      PID=$(echo "$PIDS" | grep "process_name" | awk '{print $2}')
                      echo $PID






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 21 '17 at 9:19









                      Shubham Khatri

                      1012




                      1012












                      • This Is needlessly complex. Why not simply call ps -eaf | grep "process_name" | awk '{print $2}'
                        – David Nedrow
                        Jul 4 at 13:25




















                      • This Is needlessly complex. Why not simply call ps -eaf | grep "process_name" | awk '{print $2}'
                        – David Nedrow
                        Jul 4 at 13:25


















                      This Is needlessly complex. Why not simply call ps -eaf | grep "process_name" | awk '{print $2}'
                      – David Nedrow
                      Jul 4 at 13:25






                      This Is needlessly complex. Why not simply call ps -eaf | grep "process_name" | awk '{print $2}'
                      – David Nedrow
                      Jul 4 at 13:25












                      up vote
                      0
                      down vote













                      this line gives you back the pid (process id) excluding "grep"



                      PID=$(ps aux | grep '/usr/bin/python2.7 manage.py SES__boto3_sqs_read' | grep -v grep)





                      share|improve this answer























                      • Some explanation would be nice.
                        – davidbaumann
                        Nov 19 at 15:03










                      • This is basically the same as the accepted answer, but with different decoration.
                        – Scott
                        Nov 19 at 20:29















                      up vote
                      0
                      down vote













                      this line gives you back the pid (process id) excluding "grep"



                      PID=$(ps aux | grep '/usr/bin/python2.7 manage.py SES__boto3_sqs_read' | grep -v grep)





                      share|improve this answer























                      • Some explanation would be nice.
                        – davidbaumann
                        Nov 19 at 15:03










                      • This is basically the same as the accepted answer, but with different decoration.
                        – Scott
                        Nov 19 at 20:29













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      this line gives you back the pid (process id) excluding "grep"



                      PID=$(ps aux | grep '/usr/bin/python2.7 manage.py SES__boto3_sqs_read' | grep -v grep)





                      share|improve this answer














                      this line gives you back the pid (process id) excluding "grep"



                      PID=$(ps aux | grep '/usr/bin/python2.7 manage.py SES__boto3_sqs_read' | grep -v grep)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 19 at 19:31

























                      answered Nov 19 at 14:37









                      Brian Sanchez

                      1012




                      1012












                      • Some explanation would be nice.
                        – davidbaumann
                        Nov 19 at 15:03










                      • This is basically the same as the accepted answer, but with different decoration.
                        – Scott
                        Nov 19 at 20:29


















                      • Some explanation would be nice.
                        – davidbaumann
                        Nov 19 at 15:03










                      • This is basically the same as the accepted answer, but with different decoration.
                        – Scott
                        Nov 19 at 20:29
















                      Some explanation would be nice.
                      – davidbaumann
                      Nov 19 at 15:03




                      Some explanation would be nice.
                      – davidbaumann
                      Nov 19 at 15:03












                      This is basically the same as the accepted answer, but with different decoration.
                      – Scott
                      Nov 19 at 20:29




                      This is basically the same as the accepted answer, but with different decoration.
                      – Scott
                      Nov 19 at 20:29


















                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f409655%2fexcluding-grep-from-process-list%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

                      AnyDesk - Fatal Program Failure

                      How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

                      QoS: MAC-Priority for clients behind a repeater