Skip to content

Instantly share code, notes, and snippets.

@abeluck
Forked from sschuberth/adb_kill.py
Created August 9, 2012 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abeluck/3307565 to your computer and use it in GitHub Desktop.
Save abeluck/3307565 to your computer and use it in GitHub Desktop.
kill a running android process without eclipse
#!/usr/bin/python -O
# Kill a running Android JDWP (debuggable) process without Eclipse.
import sys
import socket
import struct
import uuid
import random
import subprocess
from os.path import basename
if len(sys.argv) != 4:
print 'Usage: %s <path to adb> <device id> <process id>' % basename(sys.argv[0])
sys.exit(1)
adb = sys.argv[1]
device_id = sys.argv[2]
pid = sys.argv[3]
forward_port = 51111
print 'Forwarding PID %s to port %d ...' % (pid, forward_port)
# adb -s <device id> forward tcp:<port> jdwp:<pid>
subprocess.Popen([adb, '-s', device_id, 'forward', 'tcp:%d' % forward_port, 'jdwp:%s' % pid]).poll()
print 'Connecting via JDWP...'
try:
jdwp = socket.create_connection(('', forward_port))
jdwp.settimeout(5.0)
except socket.error, msg:
print msg
print 'Please make sure that you have closed Eclipse!'
sys.exit(1)
HANDSHAKE = 'JDWP-Handshake'
print 'Sending handshake "%s"...' % HANDSHAKE
jdwp.sendall(HANDSHAKE)
reponse = jdwp.recv(len(HANDSHAKE))
print 'Received handshake "%s"' % reponse
if reponse != HANDSHAKE:
print 'Error: Incorrect handshake. Check "adb jdwp" to make sure the PID is listed.'
jdwp.close()
sys.exit(1)
DDMS_CMD = 0x01
DDMS_CMD_SET = 0xc7
# Just a random 32 bit integer should be good enough.
packet_id = uuid.uuid4().time_low
packet_len = 23
# The string EXIT as an integer.
EXIT = 1163413844
EXIT_LEN = 4
exit_code = 1
print 'Sending EXIT command...'
packet = struct.pack('!2I3B3I', packet_len, packet_id, 0, DDMS_CMD_SET, DDMS_CMD, EXIT, EXIT_LEN, exit_code)
jdwp.send(packet)
print 'Successfully killed process %s!' % pid
jdwp.close()
#!/bin/sh
if [ $# -ne 1 ]; then
echo "Usage : $(basename $0) <package>"
exit 1
fi
if ! adb=$(which adb); then
echo "Please make sure adb is in your PATH."
exit 2
fi
did=$(adb devices | awk 'NR>1{print $1}')
pid=$(adb shell ps | grep $1 | awk '{print $2}')
for p in $pid; do
adb_kill.py $adb $did $p
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment