blob: a9f55d3e83f4d6523c8b8db31c7692f85b86551e [file] [log] [blame]
Josh Gao3dffb842018-06-14 16:12:12 -07001#!/bin/bash
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# Get the pid of processes matching a string.
Josh Gao4df62792018-06-14 16:32:12 -070017EXACT=0
Josh Gao3dffb842018-06-14 16:12:12 -070018if [ "$1" = "--exact" ]; then
Josh Gao4df62792018-06-14 16:32:12 -070019 EXACT=1
Josh Gao3dffb842018-06-14 16:12:12 -070020 shift
21fi
Josh Gao4df62792018-06-14 16:32:12 -070022
Josh Gao3dffb842018-06-14 16:12:12 -070023EXE="$1"
Josh Gao4df62792018-06-14 16:32:12 -070024if ! [ "$EXE" ] ; then
Josh Gao3dffb842018-06-14 16:12:12 -070025 echo "usage: pid [--exact] <process name>"
26 exit 255
27fi
28
Josh Gao4df62792018-06-14 16:32:12 -070029if [ $EXACT == 1 ]; then
30 PIDS="$(adb shell pidof $EXE)"
31 RC=$?
32else
33 PIDS=$(adb shell "ps -o PID,NAME | tail -n +1 | grep $EXE | tr -s ' ' | cut -f2 -d' '")
34 [ -n "$PIDS" ]
35 RC=$?
36fi
37
38for PID in $PIDS; do
39 echo $PID
40done
41
42exit $RC