0

There is a persistent process which I desire to persistently kill. To do so, I made a very simple python script:

import os
import signal
While True:
    os.system("ps >> /sdcard/newdata.txt")
    file = open("/sdcard/newdata.txt","r")
    for x in file:
        if (len(x.split())<9):
            continue
        elif (x.split()[8] == "/system/bin/XXXX"):
            pid = int(x.split()[1])
            os.kill(pid, signal.SIGKILL)
            break
    file.close()

the XXXX represents the name of the binary of the file which triggers the process. In my specific case this binary, XXXX, is a good way of recognizing the process I want to kill.

I think it should work, but instead it returns an error:

"ProcessLookupError: [Errno 3] No such process"

I printed the pid and ran the script multiple times. The pid is fixed which should mean the process didn't respawn, still it always returns an error as if the pid is wrong.

Can anyone explain this?

hellofriends
  • 123
  • 5
  • 1
    Print your `ps` output to see that probably it is not what you want, also inside python script print `x.split()[1]` to see probably it is not a pid. You have to use `ps -e` or `ps aux` or `pgrep` or `pkill`, depending on what is that XXXX you match, part of the process? of the full command? etc. – thanasisp Sep 09 '20 at 03:23
  • Your question is tagged as linux, what is that `/system/bin/`, andoid? What is your OS? – thanasisp Sep 09 '20 at 04:17
  • @thanasisp you got me, it is android – hellofriends Sep 09 '20 at 11:03
  • @thanasisp of course I printed the variables. The pid matches the one from ps output. I don't quite get why using 'ps-e' or 'ps-aux' or 'pgrep' would change anything – hellofriends Sep 09 '20 at 11:08
  • Because `ps` alone for me does not print so much columns, but we may have different shells. Also check `pgrep XXXX`, it should output the pids only. – thanasisp Sep 09 '20 at 11:11
  • @thanasisp ok I made a new code that works with the 'pgrep' and I'm getting a new pid different from 'ps' but when I try to kill it I get the same answer "No such process" – hellofriends Sep 09 '20 at 11:40
  • 1
    You could run only one bash command `pgrep XXXX | xargs kill -9`. – thanasisp Sep 09 '20 at 11:43
  • this command returns an error as if 'kill' is missing an argument which is weird because it should work. I retried without the "-9" on kill and it gave me the same error – hellofriends Sep 09 '20 at 11:50
  • Hm.. this has to be android specific, try without -9, and try more search for killing a pid in andoid. What I said is a typical way for doing this in a linux shell, and this is the only shell I could test it. – thanasisp Sep 09 '20 at 11:57
  • @thanasisp thanks for your help, this situation is very weird. To be unable to use the terminal to find pids – hellofriends Sep 09 '20 at 12:00
  • Maybe an android user could provide some more help on this. Good luck! – thanasisp Sep 09 '20 at 12:06
  • @thanasisp I think the code with 'pgrep' worked now no idea why tho – hellofriends Sep 09 '20 at 13:41

0 Answers0