Sweet
Sweet

Reputation: 13

[Android]How to pass a process number to cmd line "adb shell kill "

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.

Upvotes: 1

Views: 1245

Answers (1)

Inian
Inian

Reputation: 85690

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 "{}"

Upvotes: 1

Related Questions