adb: Find PID from the adb shell - android

I am trying to get the PID of the process INSIDE adb shell. So, I am doing adb shell which gets me to the android shell. Now, if I were to get the PID using a regular shell I would use
adb shell ps | grep android.process.acore | sed 's/\s\s*/ /g' | cut -d ' ' -f 2
OR
adb shell ps | grep android.process.acore | awk '{ print $2 }'
I get the PID (a numeric number - 2nd field of the ps | grep android.process.acore) output.
However, if I run the above commands inside android shell(after doing adb shell), I get /system/bin/sh: sed: not found and /system/bin/sh: awk: not found errors respectively. Which means, these commands are not available inside adb shell. However, grep works.
The output of the ps | grep android.process.acore inside adb shell is:
XXX_x21 11826 441 502296 39028 ffffffff 4010ff6c S android.process.acore
I am looking for the number 11826.
How can I extract it inside adb shell?
Also, please help if there is a direct way to get the PID inside the adb shell.
Regards,
Rumit

Android versions starting with 6.0 already include pidof utility:
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)

Not sure if you can get the PID directly however you can try the following
set `ps |grep android.process.acore`
echo $2
This has the affect of setting the output of the ps command into variables $1, $2, $3 etc. The PID value is in $2

I tried this one and it seems to work:
adb shell "set "ps | grep android.process.media"; kill -9 $2"

adb shell pidof [package name]
or
adb shell pidof -s [package name]
-s option is for single shot, returning only one pid.

I tried this one and it seems to work:
adb shell
ps -A | grep "android.process.acore"

Related

Get the exact value from android getprop

I wish to get specific value of properties from adb command getprop
To get specific value I use:
adb shell getprop ro.product.display_name
adb shell getprop ro.bootloader
adb shell getprop ro.serialno
adb shell getprop ro.product.model
Result example:
Galaxy Watch5
RFT4JD6GHK
RGHJKABVGTS
SM-R860
But this is not good, cos for each value, I need to do getprop each time. I wish to ask it once per device and then parse the result to get values.
Something like:
adb shell getprop
and then what I get from stdout is long list with all properties and values
...
[ro.build.version.release]: [4.2.2]
[ro.product.display_name]: [Galaxy Watch5]
[ro.bootloader]: [RFT4JD6GHK]
[ro.hardware]: [qcom]
[ro.opengles.version]: [196108]
[ro.product.brand]: [Verizon]
[ro.product.manufacturer]: [samsung]
[ro.serialno]: [RGHJKABVGTS]
[ro.product.model]: [SM-R860]
...
How to parse this big list end get same result as separate commands? I try bellow but I get the whole thing with brackets:
adb shell getprop | grep "ro.product.display_name\|ro.bootloader\|ro.serialno\|ro.product.model"
Result:
[ro.product.display_name]: [Galaxy Watch5]
[ro.bootloader]: [RFT4JD6GHK]
[ro.serialno]: [RGHJKABVGTS]
[ro.product.model]: [SM-R860]
I solve it, but I'm not sure if there is easier solution:
adb shell getprop | grep 'ro.product.display_name\|ro.bootloader\|ro.serialno\|ro.product.model' | cut -d ":" -f 2 | sed 's:^..\(.*\).$:\1:'
Using awk and defining an array with the keys you want (ro.product.display_name used as an example here), you can do
adb shell getprop | awk -F '[][:]' 'BEGIN {keys["ro.product.display_name"]++} $2 in keys {print $5}'

Finding substring at the end of line in shell

I am new to shell scripting.
When I write
adb shell ps | grep "org.mozilla.fennec"
I get
u0_a52 908 57 557664 144820 ffffffff b6f755cc S org.mozilla.fennec
and
u0_a52 1083 57 243108 23824 ffffffff b6f755cc S org.mozilla.fennec.UpdateService
The problem is I only need the first line. So I tried with adb shell ps | grep "org.mozilla.fennec$" but surprisingly it shows nothing.
I will also need the second field of the first line. How can I do this using grep?
Any help will be appreciated. Thanks.
To print 2nd field you can use awk:
adb shell ps | awk -v RS='\r' '$NF == "org.mozilla.fennec"{print $2}'
To print whole line:
adb shell ps | awk -v RS='\r' '$NF == "org.mozilla.fennec"'
To remove second line:
adb shell ps | grep "org.mozilla.fennec"|grep -v "UpdateService"
To print second field:
adb shell ps | grep "org.mozilla.fennec"|grep -v "UpdateService"| awk '{print $2}'
The below command will look first occurrence of org.mozilla.fennec in command output of "adb shell ps" and will stop looking after that.
adb shell ps | awk '{if($0~/org.mozilla.fennec/){print $2; exit 0}}'
Its better when you need the first pattern match, don't need to scan whole output after first match.

Android ADB commands to get the device properties

I am trying to get the device properties from ADB commands. I can how ever get those values by running sample android application. How ever I wish to get using adb shell command itself to make my life easier. Here is the way I will get through sample application but I want corresponding adb commands for
device manufacturer
device hardware
device model
Os version(integer value)
Kernel version
* Please note my device is not rooted and I have no idea of rooting the device to get these values :-) *
## Code snippet
import android.os.Build;
manufacturer = Build.MANUFACTURER;
hardware = Build.HARDWARE;
model = Build.MODEL;
oSVersion = Build.VERSION.SDK_INT;
kernelVersion = System.getProperty("os.version");
However I can able to get the os version. But then I want SDK version in integer. I want 18 in place of 4.2.2
C:\>adb shell getprop ro.build.version.release
4.2.2
adb shell getprop ro.build.version.sdk
If you want to see the whole list of parameters just type:
adb shell getprop
From Linux Terminal:
adb shell getprop | grep "model\|version.sdk\|manufacturer\|hardware\|platform\|revision\|serialno\|product.name\|brand"
From Windows PowerShell:
adb shell
getprop | grep -e 'model' -e 'version.sdk' -e 'manufacturer' -e 'hardware' -e 'platform' -e 'revision' -e 'serialno' -e 'product.name' -e 'brand'
Sample output for Samsung:
[gsm.version.baseband]: [G900VVRU2BOE1]
[gsm.version.ril-impl]: [Samsung RIL v3.0]
[net.knoxscep.version]: [2.0.1]
[net.knoxsso.version]: [2.1.1]
[net.knoxvpn.version]: [2.2.0]
[persist.service.bdroid.version]: [4.1]
[ro.board.platform]: [msm8974]
[ro.boot.hardware]: [qcom]
[ro.boot.serialno]: [xxxxxx]
[ro.build.version.all_codenames]: [REL]
[ro.build.version.codename]: [REL]
[ro.build.version.incremental]: [G900VVRU2BOE1]
[ro.build.version.release]: [5.0]
[ro.build.version.sdk]: [21]
[ro.build.version.sdl]: [2101]
[ro.com.google.gmsversion]: [5.0_r2]
[ro.config.timaversion]: [3.0]
[ro.hardware]: [qcom]
[ro.opengles.version]: [196108]
[ro.product.brand]: [Verizon]
[ro.product.manufacturer]: [samsung]
[ro.product.model]: [SM-G900V]
[ro.product.name]: [kltevzw]
[ro.revision]: [14]
[ro.serialno]: [e5ce97c7]
You should use adb shell getprop command and grep specific info about your current device, For additional information you can read documentation:
Android Debug Bridge documentation
I added some examples below:
language - adb shell getprop | grep language
[persist.sys.language]: [en]
[ro.product.locale.language]: [en]
boot complete ( device ready after reset) - adb shell getprop | grep boot_completed
[sys.boot_completed]: [1]
device model - adb shell getprop | grep model
[ro.product.model]: [Nexus 4]
sdk version - adb shell getprop | grep sdk
[ro.build.version.sdk]: [22]
time zone - adb shell getprop | grep timezone
[persist.sys.timezone]: [Asia/China]
serial number - adb shell getprop | grep serialno
[ro.boot.serialno]: [1234567]
For Power-Shell
./adb shell getprop | Select-String -Pattern '(model)|(version.sdk)|(manufacturer)|(platform)|(serialno)|(product.name)|(brand)'
For Linux (burrowing answer from #0x8BADF00D)
adb shell getprop | grep "model\|version.sdk\|manufacturer\|hardware\|platform\|revision\|serialno\|product.name\|brand"
For single string find in power shell
./adb shell getprop | Select-String -Pattern 'model'
or
./adb shell getprop | Select-String -Pattern '(model)'
For multiple
./adb shell getprop | Select-String -Pattern '(a|b|c|d)'

How to filter Android adb shell logcat using multiple tags at one time?

As we know, we can use the command to filter the log in Android:
adb shell logcat | grep "tag1"
Then, the logs which contain the tag1 will be output.
Now, I want to use one command that can output the logs which contain the tag1 and tag2.
I'm try to use, but it cannot work.
adb shell logcat | grep "tag1 tag2"
How to do it?
This is how to use grep properly:
adb shell logcat | grep -E "tag1|tag2"
Or you could just use logcat's built-in filters:
adb logcat -s tag1:* tag2:*

How to run logcat on multiple devices?

How can I run logcat on multiple devices at the same time? "adb logcat" command gives an error:
error: more than one device and emulator
Use the -s option of adb:
adb -s <serialnumber>
Example
C:\Users\lel>adb devices
List of devices attached
192.168.198.101:5555 device
0123456789ABCDEF device
adb -s 0123456789ABCDEF logcat
adb -s 192.168.198.101:5555 logcat
You can combine grep whit this, to get all lines that contain it.
an example is with System.out
Example:
adb -s 192.168.198.101:5555 logcat | grep "System.out"
I thought it might be useful. I have this script that helps me a lot. It logcats each device to a different file. To stop logging just press CTRL+C.
#! /bin/bash
devices=`adb devices | grep 'device$' | cut -f1`
pids=""
for device in $devices
do
log_file="$device-`date +%d-%m-%H:%M:%S`.log"
echo "Logging device $device to \"$log_file\""
adb -s $device logcat -v threadtime > $log_file &
pids="$pids $!"
done
echo "Children PIDs: $pids"
killemall()
{
echo "Killing children (what a shame...)"
for pid in $pids
do
echo "Killing $pid"
kill -TERM $pid
done
}
trap killemall INT
wait
Use your device ip:
adb -s device_ip:5555

Categories

Resources