srooth
srooth

Reputation: 43

How to get the PID from PS command output in Android shell

Can anyone tell how can I get the PID from the output of PS command in Android shell. For example from the output:

u0_a51    20240 38    132944 22300 ffffffff 40037ebc S com.example.poc_service

pid value 20240 is to be got. I tried

ps -ef | grep com.example.poc_service

but to no avail. Also pgrep is not being recognized.

Upvotes: 1

Views: 8114

Answers (3)

alijandro
alijandro

Reputation: 12167

Neither grep, egrep, fgrep, rgrep is available in Android.

If you are working on Unix, Linux, Mac or Cygwin, you can pipe the output of adb shell command to get the result you want.

$ adb shell ps |grep settings
system    23846 71    111996 22676 ffffffff 00000000 S com.android.settings
$ adb shell ps |grep settings |awk '{print $2}'
23846

Upvotes: 1

not2qubit
not2qubit

Reputation: 17037

If you have shell access in Android you can also use pidof:

# pidof com.example.poc_service
20240 

However, be careful as there may be multiple processes matching...

Upvotes: 3

Nick
Nick

Reputation: 930

Its pretty nasty but it works:

for pid in `ls /proc`; do 
   cmd=`cat $pid/cmdline 2> /dev/null`;

   if [ "X$cmd" == "Xcom.example.poc_service" ]; then
       echo $pid;
   fi 
done

or as one line:

for pid in `ls /proc`; do cmd=`cat $pid/cmdline 2> /dev/null`; if [ "X$cmd" == "X/system/bin/mm-qcamera-daemon" ]; then echo $pid; fi done

Upvotes: 0

Related Questions