Find out the running process ID by package name











up vote
11
down vote

favorite
6












I am working on a script in which I need to supply the PID of my application. I am able to list all the processes with their PIDs by following command and could see the entry of my application.




adb shell ps




This gives me a huge list of processes. And I need a single entry (which I can further supply to another command), so I want to filter this results with a package name. The grep command does not work on my windows machine. Also tried following command but it didn't help.




adb shell ps name:my_app_package











share|improve this question




























    up vote
    11
    down vote

    favorite
    6












    I am working on a script in which I need to supply the PID of my application. I am able to list all the processes with their PIDs by following command and could see the entry of my application.




    adb shell ps




    This gives me a huge list of processes. And I need a single entry (which I can further supply to another command), so I want to filter this results with a package name. The grep command does not work on my windows machine. Also tried following command but it didn't help.




    adb shell ps name:my_app_package











    share|improve this question


























      up vote
      11
      down vote

      favorite
      6









      up vote
      11
      down vote

      favorite
      6






      6





      I am working on a script in which I need to supply the PID of my application. I am able to list all the processes with their PIDs by following command and could see the entry of my application.




      adb shell ps




      This gives me a huge list of processes. And I need a single entry (which I can further supply to another command), so I want to filter this results with a package name. The grep command does not work on my windows machine. Also tried following command but it didn't help.




      adb shell ps name:my_app_package











      share|improve this question















      I am working on a script in which I need to supply the PID of my application. I am able to list all the processes with their PIDs by following command and could see the entry of my application.




      adb shell ps




      This gives me a huge list of processes. And I need a single entry (which I can further supply to another command), so I want to filter this results with a package name. The grep command does not work on my windows machine. Also tried following command but it didn't help.




      adb shell ps name:my_app_package








      android adb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '16 at 17:25









      Alex P.

      19.6k1366116




      19.6k1366116










      asked Mar 25 '13 at 6:25









      Mukesh Bhojwani

      96431226




      96431226
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          17
          down vote













          Since Android 7.0 the easiest way to find out the process ID by package name is to use pidof command:



          usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME]...

          Print the PIDs of all processes with the given names.

          -s single shot, only return one pid.
          -o omit PID(s)


          Just run it like this:



          adb shell pidof my.app.package


          In Android before 7.0 people used ps command and then parsed its output using either built-in filter by comm value (which for android apps is the last 15 characters of the package name) or grep command. The comm filter did not work if the last 15 characters of the name started with a digit and the grep was not included by default until Android 4.2. But even after the proper process line was found the PID value still needed to be extracted.



          There were multiple ways to do that. Here is how finding the process and extracting PID could be done with a single sed command:



          adb shell "ps | sed -n 's/^[^ ]* *([0-9]*).* my.app.package$/1/p'"


          Again the problem is that sed was was not included by default until Android 6.0.



          But if you must use an older device you can always use the following Android version independent solution. It does not use any external commands - just Android shell built-ins:



          adb shell "for p in /proc/[0-9]*; do [[ $(<$p/cmdline) = my.app.package ]] && echo ${p##*/}; done"


          The most popular reason for looking for a PID is to use it in some other command like kill. Let's say we have multiple instances of logcat running and we want to finish them all gracefully at once. Just replace the echo with kill -2 in the last command:



          adb shell "for p in /proc/[0-9]*; do [[ $(<$p/cmdline) = logcat ]] && kill -2 ${p##*/}; done"


          Replace " with ' if running the commands from Linux/OSX shell.






          share|improve this answer



















          • 1




            Any way to use their full package name, for long ones?
            – slezica
            Jul 20 '14 at 21:01










          • ps <comm> doesn't work if name is a bit longer, weird.
            – yorkw
            Sep 3 '14 at 3:19


















          up vote
          9
          down vote













          Instead of using adb shell ps, firstly enter adb shell and then use ps.



          Step by step:




          1. Enter adb shell command while a device (or emulator) is connected.
            (Command line prefix will be shell@android:/ $ after executing this command.)


          2. Enter ps | grep <package_name_to_be_filtered> (i.e. ps | grep com.google).






          C:> adb shell
          shell@android:/ $ ps | grep com.google
          u0_a64 3353 2467 903744 52904 ffffffff 00000000 S com.google.process.location
          u0_a64 3426 2467 893964 49452 ffffffff 00000000 S com.google.process.gapps





          share|improve this answer























          • That returns nothing for me. p.s. I connect to an emulator which uses API 26.
            – talha06
            Sep 9 at 22:46


















          up vote
          -1
          down vote













          The processes shown by ps can be limited to those belonging to any given user by piping the output through grep, a filter that is used for searching text. For example, processes belonging to a user with a username adam can be displayed with the following:



          ps -ef | grep adam


          The -e option generates a list of information about every process currently running. The -f option generates a listing that contains fewer items of information for each process than the -l option.






          share|improve this answer

















          • 1




            listing all processes is the default behavior for ps in android, so -e is not needed. and -f is not supported
            – Alex P.
            Mar 25 '13 at 19:09












          • +1 while -ef may be redundant using grep is good advice - can use for custom filters...
            – Dori
            Apr 7 '14 at 11:49











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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%2fstackoverflow.com%2fquestions%2f15608876%2ffind-out-the-running-process-id-by-package-name%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








          up vote
          17
          down vote













          Since Android 7.0 the easiest way to find out the process ID by package name is to use pidof command:



          usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME]...

          Print the PIDs of all processes with the given names.

          -s single shot, only return one pid.
          -o omit PID(s)


          Just run it like this:



          adb shell pidof my.app.package


          In Android before 7.0 people used ps command and then parsed its output using either built-in filter by comm value (which for android apps is the last 15 characters of the package name) or grep command. The comm filter did not work if the last 15 characters of the name started with a digit and the grep was not included by default until Android 4.2. But even after the proper process line was found the PID value still needed to be extracted.



          There were multiple ways to do that. Here is how finding the process and extracting PID could be done with a single sed command:



          adb shell "ps | sed -n 's/^[^ ]* *([0-9]*).* my.app.package$/1/p'"


          Again the problem is that sed was was not included by default until Android 6.0.



          But if you must use an older device you can always use the following Android version independent solution. It does not use any external commands - just Android shell built-ins:



          adb shell "for p in /proc/[0-9]*; do [[ $(<$p/cmdline) = my.app.package ]] && echo ${p##*/}; done"


          The most popular reason for looking for a PID is to use it in some other command like kill. Let's say we have multiple instances of logcat running and we want to finish them all gracefully at once. Just replace the echo with kill -2 in the last command:



          adb shell "for p in /proc/[0-9]*; do [[ $(<$p/cmdline) = logcat ]] && kill -2 ${p##*/}; done"


          Replace " with ' if running the commands from Linux/OSX shell.






          share|improve this answer



















          • 1




            Any way to use their full package name, for long ones?
            – slezica
            Jul 20 '14 at 21:01










          • ps <comm> doesn't work if name is a bit longer, weird.
            – yorkw
            Sep 3 '14 at 3:19















          up vote
          17
          down vote













          Since Android 7.0 the easiest way to find out the process ID by package name is to use pidof command:



          usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME]...

          Print the PIDs of all processes with the given names.

          -s single shot, only return one pid.
          -o omit PID(s)


          Just run it like this:



          adb shell pidof my.app.package


          In Android before 7.0 people used ps command and then parsed its output using either built-in filter by comm value (which for android apps is the last 15 characters of the package name) or grep command. The comm filter did not work if the last 15 characters of the name started with a digit and the grep was not included by default until Android 4.2. But even after the proper process line was found the PID value still needed to be extracted.



          There were multiple ways to do that. Here is how finding the process and extracting PID could be done with a single sed command:



          adb shell "ps | sed -n 's/^[^ ]* *([0-9]*).* my.app.package$/1/p'"


          Again the problem is that sed was was not included by default until Android 6.0.



          But if you must use an older device you can always use the following Android version independent solution. It does not use any external commands - just Android shell built-ins:



          adb shell "for p in /proc/[0-9]*; do [[ $(<$p/cmdline) = my.app.package ]] && echo ${p##*/}; done"


          The most popular reason for looking for a PID is to use it in some other command like kill. Let's say we have multiple instances of logcat running and we want to finish them all gracefully at once. Just replace the echo with kill -2 in the last command:



          adb shell "for p in /proc/[0-9]*; do [[ $(<$p/cmdline) = logcat ]] && kill -2 ${p##*/}; done"


          Replace " with ' if running the commands from Linux/OSX shell.






          share|improve this answer



















          • 1




            Any way to use their full package name, for long ones?
            – slezica
            Jul 20 '14 at 21:01










          • ps <comm> doesn't work if name is a bit longer, weird.
            – yorkw
            Sep 3 '14 at 3:19













          up vote
          17
          down vote










          up vote
          17
          down vote









          Since Android 7.0 the easiest way to find out the process ID by package name is to use pidof command:



          usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME]...

          Print the PIDs of all processes with the given names.

          -s single shot, only return one pid.
          -o omit PID(s)


          Just run it like this:



          adb shell pidof my.app.package


          In Android before 7.0 people used ps command and then parsed its output using either built-in filter by comm value (which for android apps is the last 15 characters of the package name) or grep command. The comm filter did not work if the last 15 characters of the name started with a digit and the grep was not included by default until Android 4.2. But even after the proper process line was found the PID value still needed to be extracted.



          There were multiple ways to do that. Here is how finding the process and extracting PID could be done with a single sed command:



          adb shell "ps | sed -n 's/^[^ ]* *([0-9]*).* my.app.package$/1/p'"


          Again the problem is that sed was was not included by default until Android 6.0.



          But if you must use an older device you can always use the following Android version independent solution. It does not use any external commands - just Android shell built-ins:



          adb shell "for p in /proc/[0-9]*; do [[ $(<$p/cmdline) = my.app.package ]] && echo ${p##*/}; done"


          The most popular reason for looking for a PID is to use it in some other command like kill. Let's say we have multiple instances of logcat running and we want to finish them all gracefully at once. Just replace the echo with kill -2 in the last command:



          adb shell "for p in /proc/[0-9]*; do [[ $(<$p/cmdline) = logcat ]] && kill -2 ${p##*/}; done"


          Replace " with ' if running the commands from Linux/OSX shell.






          share|improve this answer














          Since Android 7.0 the easiest way to find out the process ID by package name is to use pidof command:



          usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME]...

          Print the PIDs of all processes with the given names.

          -s single shot, only return one pid.
          -o omit PID(s)


          Just run it like this:



          adb shell pidof my.app.package


          In Android before 7.0 people used ps command and then parsed its output using either built-in filter by comm value (which for android apps is the last 15 characters of the package name) or grep command. The comm filter did not work if the last 15 characters of the name started with a digit and the grep was not included by default until Android 4.2. But even after the proper process line was found the PID value still needed to be extracted.



          There were multiple ways to do that. Here is how finding the process and extracting PID could be done with a single sed command:



          adb shell "ps | sed -n 's/^[^ ]* *([0-9]*).* my.app.package$/1/p'"


          Again the problem is that sed was was not included by default until Android 6.0.



          But if you must use an older device you can always use the following Android version independent solution. It does not use any external commands - just Android shell built-ins:



          adb shell "for p in /proc/[0-9]*; do [[ $(<$p/cmdline) = my.app.package ]] && echo ${p##*/}; done"


          The most popular reason for looking for a PID is to use it in some other command like kill. Let's say we have multiple instances of logcat running and we want to finish them all gracefully at once. Just replace the echo with kill -2 in the last command:



          adb shell "for p in /proc/[0-9]*; do [[ $(<$p/cmdline) = logcat ]] && kill -2 ${p##*/}; done"


          Replace " with ' if running the commands from Linux/OSX shell.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 10 '16 at 4:17

























          answered Mar 25 '13 at 19:04









          Alex P.

          19.6k1366116




          19.6k1366116








          • 1




            Any way to use their full package name, for long ones?
            – slezica
            Jul 20 '14 at 21:01










          • ps <comm> doesn't work if name is a bit longer, weird.
            – yorkw
            Sep 3 '14 at 3:19














          • 1




            Any way to use their full package name, for long ones?
            – slezica
            Jul 20 '14 at 21:01










          • ps <comm> doesn't work if name is a bit longer, weird.
            – yorkw
            Sep 3 '14 at 3:19








          1




          1




          Any way to use their full package name, for long ones?
          – slezica
          Jul 20 '14 at 21:01




          Any way to use their full package name, for long ones?
          – slezica
          Jul 20 '14 at 21:01












          ps <comm> doesn't work if name is a bit longer, weird.
          – yorkw
          Sep 3 '14 at 3:19




          ps <comm> doesn't work if name is a bit longer, weird.
          – yorkw
          Sep 3 '14 at 3:19












          up vote
          9
          down vote













          Instead of using adb shell ps, firstly enter adb shell and then use ps.



          Step by step:




          1. Enter adb shell command while a device (or emulator) is connected.
            (Command line prefix will be shell@android:/ $ after executing this command.)


          2. Enter ps | grep <package_name_to_be_filtered> (i.e. ps | grep com.google).






          C:> adb shell
          shell@android:/ $ ps | grep com.google
          u0_a64 3353 2467 903744 52904 ffffffff 00000000 S com.google.process.location
          u0_a64 3426 2467 893964 49452 ffffffff 00000000 S com.google.process.gapps





          share|improve this answer























          • That returns nothing for me. p.s. I connect to an emulator which uses API 26.
            – talha06
            Sep 9 at 22:46















          up vote
          9
          down vote













          Instead of using adb shell ps, firstly enter adb shell and then use ps.



          Step by step:




          1. Enter adb shell command while a device (or emulator) is connected.
            (Command line prefix will be shell@android:/ $ after executing this command.)


          2. Enter ps | grep <package_name_to_be_filtered> (i.e. ps | grep com.google).






          C:> adb shell
          shell@android:/ $ ps | grep com.google
          u0_a64 3353 2467 903744 52904 ffffffff 00000000 S com.google.process.location
          u0_a64 3426 2467 893964 49452 ffffffff 00000000 S com.google.process.gapps





          share|improve this answer























          • That returns nothing for me. p.s. I connect to an emulator which uses API 26.
            – talha06
            Sep 9 at 22:46













          up vote
          9
          down vote










          up vote
          9
          down vote









          Instead of using adb shell ps, firstly enter adb shell and then use ps.



          Step by step:




          1. Enter adb shell command while a device (or emulator) is connected.
            (Command line prefix will be shell@android:/ $ after executing this command.)


          2. Enter ps | grep <package_name_to_be_filtered> (i.e. ps | grep com.google).






          C:> adb shell
          shell@android:/ $ ps | grep com.google
          u0_a64 3353 2467 903744 52904 ffffffff 00000000 S com.google.process.location
          u0_a64 3426 2467 893964 49452 ffffffff 00000000 S com.google.process.gapps





          share|improve this answer














          Instead of using adb shell ps, firstly enter adb shell and then use ps.



          Step by step:




          1. Enter adb shell command while a device (or emulator) is connected.
            (Command line prefix will be shell@android:/ $ after executing this command.)


          2. Enter ps | grep <package_name_to_be_filtered> (i.e. ps | grep com.google).






          C:> adb shell
          shell@android:/ $ ps | grep com.google
          u0_a64 3353 2467 903744 52904 ffffffff 00000000 S com.google.process.location
          u0_a64 3426 2467 893964 49452 ffffffff 00000000 S com.google.process.gapps






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 16 '17 at 13:59









          FredMaggiowski

          1,74821333




          1,74821333










          answered Dec 16 '14 at 11:53









          Devrim

          11.7k34563




          11.7k34563












          • That returns nothing for me. p.s. I connect to an emulator which uses API 26.
            – talha06
            Sep 9 at 22:46


















          • That returns nothing for me. p.s. I connect to an emulator which uses API 26.
            – talha06
            Sep 9 at 22:46
















          That returns nothing for me. p.s. I connect to an emulator which uses API 26.
          – talha06
          Sep 9 at 22:46




          That returns nothing for me. p.s. I connect to an emulator which uses API 26.
          – talha06
          Sep 9 at 22:46










          up vote
          -1
          down vote













          The processes shown by ps can be limited to those belonging to any given user by piping the output through grep, a filter that is used for searching text. For example, processes belonging to a user with a username adam can be displayed with the following:



          ps -ef | grep adam


          The -e option generates a list of information about every process currently running. The -f option generates a listing that contains fewer items of information for each process than the -l option.






          share|improve this answer

















          • 1




            listing all processes is the default behavior for ps in android, so -e is not needed. and -f is not supported
            – Alex P.
            Mar 25 '13 at 19:09












          • +1 while -ef may be redundant using grep is good advice - can use for custom filters...
            – Dori
            Apr 7 '14 at 11:49















          up vote
          -1
          down vote













          The processes shown by ps can be limited to those belonging to any given user by piping the output through grep, a filter that is used for searching text. For example, processes belonging to a user with a username adam can be displayed with the following:



          ps -ef | grep adam


          The -e option generates a list of information about every process currently running. The -f option generates a listing that contains fewer items of information for each process than the -l option.






          share|improve this answer

















          • 1




            listing all processes is the default behavior for ps in android, so -e is not needed. and -f is not supported
            – Alex P.
            Mar 25 '13 at 19:09












          • +1 while -ef may be redundant using grep is good advice - can use for custom filters...
            – Dori
            Apr 7 '14 at 11:49













          up vote
          -1
          down vote










          up vote
          -1
          down vote









          The processes shown by ps can be limited to those belonging to any given user by piping the output through grep, a filter that is used for searching text. For example, processes belonging to a user with a username adam can be displayed with the following:



          ps -ef | grep adam


          The -e option generates a list of information about every process currently running. The -f option generates a listing that contains fewer items of information for each process than the -l option.






          share|improve this answer












          The processes shown by ps can be limited to those belonging to any given user by piping the output through grep, a filter that is used for searching text. For example, processes belonging to a user with a username adam can be displayed with the following:



          ps -ef | grep adam


          The -e option generates a list of information about every process currently running. The -f option generates a listing that contains fewer items of information for each process than the -l option.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 '13 at 6:43









          Hiren Pandya

          865720




          865720








          • 1




            listing all processes is the default behavior for ps in android, so -e is not needed. and -f is not supported
            – Alex P.
            Mar 25 '13 at 19:09












          • +1 while -ef may be redundant using grep is good advice - can use for custom filters...
            – Dori
            Apr 7 '14 at 11:49














          • 1




            listing all processes is the default behavior for ps in android, so -e is not needed. and -f is not supported
            – Alex P.
            Mar 25 '13 at 19:09












          • +1 while -ef may be redundant using grep is good advice - can use for custom filters...
            – Dori
            Apr 7 '14 at 11:49








          1




          1




          listing all processes is the default behavior for ps in android, so -e is not needed. and -f is not supported
          – Alex P.
          Mar 25 '13 at 19:09






          listing all processes is the default behavior for ps in android, so -e is not needed. and -f is not supported
          – Alex P.
          Mar 25 '13 at 19:09














          +1 while -ef may be redundant using grep is good advice - can use for custom filters...
          – Dori
          Apr 7 '14 at 11:49




          +1 while -ef may be redundant using grep is good advice - can use for custom filters...
          – Dori
          Apr 7 '14 at 11:49


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f15608876%2ffind-out-the-running-process-id-by-package-name%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

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          Alexandru Averescu

          Trompette piccolo