android - Find out the running process ID by package name -
i working on script in need supply pid of application. able list processes pids following command , see entry of application.
adb shell ps
this gives me huge list of processes. , need single entry (which can further supply command), want filter results package name. grep command not work on windows machine. tried following command didn't help.
adb shell ps name:my_app_package
since android 7.0 easiest way find out process id package name use pidof
command:
usage: pidof [-s] [-o omitpid[,omitpid...]] [name]... print pids of processes given names. -s single shot, return 1 pid. -o omit pid(s)
just run this:
adb shell pidof my.app.package
in android before 7.0 people used ps
command , parsed output using either built-in filter comm
value (which android apps last 15 characters of package name) or grep
command. comm
filter did not work if last 15 characters of name started digit , grep
not included default until android 4.2. after proper process line found pid
value still needed extracted.
there multiple ways that. here how finding process , extracting pid done single sed
command:
adb shell "ps | sed -n 's/^[^ ]* *\([0-9]*\).* my\.app\.package$/\1/p'"
again problem sed
was not included default until android 6.0.
but if must use older device can use following android version independent solution. not use external commands - android shell built-ins:
adb shell "for p in /proc/[0-9]*; [[ $(<$p/cmdline) = my.app.package ]] && echo ${p##*/}; done"
the popular reason looking pid use in other command kill
. let's have multiple instances of logcat
running , want finish them gracefully @ once. replace echo
kill -2
in last command:
adb shell "for p in /proc/[0-9]*; [[ $(<$p/cmdline) = logcat ]] && kill -2 ${p##*/}; done"
replace "
'
if running commands linux/osx shell.
Comments
Post a Comment