Search code examples
androidpythonsubprocessadbpexpect

running adb with python: executing a program and ending it


I am trying to perform adb interactions through python code. I have an endless executable on the android device which i would like to start and after 10 seconds kill it. right now, i can get the program to start but cannot kill it other the manually pressing ctrl+c.

procID = subprocess.Popen(["adb", "shell"], stdin=subprocess.PIPE,)
procID.communicate('su\n endless_program data/test 5\n')
time.sleep(5)
os.kill(procID, signal.SIGINT)
procID.kill()

i tried killing it with os.kill or procID.kill but both don't seem to work.

I have also trying using pexpect, but for some reason i cant get it to run adb.


Solution

  • You are just killing the adb shell session, which won't kill the running application. If you would like to kill the running Android application, you have to stop the app over the adb shell. For details have a look at this stackoverflow answer.