1

I am trying kill the process by pid but without success yet. To get pid I use command in adb shell:

echo $(ps |grep myword) | cut -d' ' -f2

To kill pid I use command:

kill <PID>

But I cannot join that two commands.. What I've tried was:

kill $($(ps |grep debug) | cut -d' ' -f2)

but it is somehow wrong. Could I ask you for a help?

tl;dr

The problem is about joining two commands in adb shell, android console -to kill process found by name.

echo $(ps |grep myword) | cut -d' ' -f2
kill <PID>

I forgot to say: I am working on Windows7, so grep might not working in console. Really sorry for this.

I also tried this command:

adb shell "su -c 'echo $(ps | grep process_name) | cut -d' ' -f2 | xargs kill'"

This example is working well, however last part xargs fails:

adb shell ^" "su -c 'echo $(ps | grep ror)'|cut -d' ' -f2"
deadfish
  • 11,996
  • 12
  • 87
  • 136

3 Answers3

4

The process can be killed the following ways (On Linux System).

  1. With your approach, the command (on ADB shell) should be as follows:
    echo $(ps | grep process_name) | cut -d' ' -f2 | xargs kill

  2. On ADB shell
    ps | grep process_name | awk '{print $2}' | xargs kill
    On the host machine as
    adb shell ps | grep process_name | awk '{print $2}' | xargs adb shell kill

  3. You can use busybox and do it. You can also build your own command (ex. pkill), like shown here. killall is already supported by busybox.

Option one should resolve your query, I would prefer the second option.

Saurabh Meshram
  • 8,106
  • 3
  • 23
  • 32
1

The solution for command adb shell in windows7 was:

adb shell ^" "su -c 'ps | echo $(grep process_name) | cut -d " " -f2 |xargs kill '"
deadfish
  • 11,996
  • 12
  • 87
  • 136
  • there is a problem with `cut -d " " -f2` part - it would return PID only when there was exactly 1 space between UID and PID, which is not always the case. – Alex P. Aug 13 '14 at 14:56
  • you are right, it is possible to use regexp – deadfish Aug 13 '14 at 17:00
0

Kill with single adb command:

adb shell "for pid in  \`ps  | grep process_name | sed 's/  */ /g' | \
     cut -d ' '  -f 2 \`; do kill \$pid; done"
zoli2k
  • 3,388
  • 4
  • 26
  • 36