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.
Related
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}'
I would like to find a specific target's pid and kill this process.
Then, i made a shell:
#!/bin/bash
#
func(){
while true; do
adb shell ps | grep my-target-process | awk '{print $2}' | xargs adb shell kill
sleep 1
done
}
But, i always get errors like below :
usage: kill [-s signame | -signum | -signame] { job | pid | pgrp } ...
kill -l [exit_status ...]
Is there anything wrong? Thanks.
I will add an explanation the logic I suggested in the comments section. The reason why
| xargs -I{} adb shell kill "{}"
works is, with the -I{} flag in xargs, the {} becomes a placeholder for the output returned from the previous command, i.e. the process-id is now present in {} and can be passed as an argument/input to your kill command.
(also) As an alternative, if your Android version supports pidof command, which returns the process-id pid directly, you can do
adb shell pidof -s "my-target-process" | xargs -I{} kill "{}"
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"
Is it possible to check a devices orientation via ADB?
Not by installing any software, calling any existing software, just through ADB. Would guess there is a status file somewhere in /proc, but could not find it yet.
This can be done through the following command:
adb shell dumpsys | grep 'SurfaceOrientation' | awk '{ print $2 }'
The output will be an integer ranging from 0 to 3 for each of the four possible orientations. 0 and 2 are landscapes while 1 and 3 are portraits. As the dumpsys output is very large, the command might take a few seconds to complete.
Update: dgmltn's modification is likely much faster:
adb shell dumpsys input | grep 'SurfaceOrientation' | awk '{ print $2 }'
Simpler solution:
adb shell dumpsys window | grep -i surfaceorientation | awk '{ print $2 }'
I found this method content query --uri content://settings/system --projection name:value --where "name='user_rotation'" after opening adb shell . Although doesn't seem to work if entered without opening a shell first.
In addition to the previous answers, I would like to point out that the return value of adb shell dumpsys | grep 'SurfaceOrientation' does not always follow a specific rule across devices ("0 and 2 are landscapes while 1 and 3 are portraits" is wrong).
I improved the method by an additional query, which advises how to interpret the return value, namely
dumpsys window | grep 'mLandscapeRotation'
value 0 means: 0 and 2 are landscapes, 1 and 3 are portraits
value 1 means the opposite situation
I am not sure if something has changed since the last answers were written, or if it is because I am emulating a different device.
but neither SurfaceOrientation nor mLandscapeRotation showed up in dumpsys for me.
After diffing the outputs of dumpsys between rotations I found
mPredictedRotation
#Normal
$./adb shell dumpsys window displays | grep 'mPredictedRotation'
mPredictedRotation=0
#Right
$./adb shell dumpsys window displays | grep 'mPredictedRotation'
mPredictedRotation=3
#Inverted
$./adb shell dumpsys window displays | grep 'mPredictedRotation'
mPredictedRotation=2
$Left
#./adb shell dumpsys window displays | grep 'mPredictedRotation'
mPredictedRotation=1
i have many application running on my emulator but ps -ef does not show any process y?
It seems like Android's ps does not accept the argument -ef. Try issuing ps only?
adb shell ps | grep com.android.mms | awk '{print $2}'
This gives the process id of the attached process