I have a class which can start and close a process. However it seems not to close the process.
My python code, there are other methods but they work fine.:
class KismetInstance:
"""Creates a kismet_server instance"""
def __init__(self, value=False):
logging.basicConfig(format='%(asctime)-15s::: %(message)s')
self.logger = logging.getLogger('kismet_instance')
self.example = value
def __create_kismet_instance__(self):
"""
Create a kismet_server subprocess.
:return:
"""
shell = ['sudo', '/usr/local/bin/kismet_server']
self.logger.debug('Attempting to run: %s', " ".join(shell))
self.kismet = Popen(shell, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=r'./logs', preexec_fn=os.setsid)
def __destroy_kismet_instance__(self):
"""
Kill the subprocess
:return:
"""
os.killpg(os.getpgid(self.kismet.pid), 15)
It can create the subprocess fine. But I get this error when I try to kill (no sudo)
OSError: [Errno 1] Operation not permitted
And if I run with sudo, the process is still running afterwards.
pi@raspberrypi ~/project $ ps -A | grep 'kismet'
2912 ? 00:00:00 kismet_server
I managed to fix this. Turns out the subprocess was respawning itself creating something weird which prevented python from keeping track of it.
So I had to do this to fix it, However this is not the most elegant solution, and rather dangerouddangerous .
Be careful if you use this , because if you enter a term more broad than mine ( 'kismet'
) then you could kill a lot of processes on your system.
def __destroy_kismet_instance__(self):
"""
Kill the subprocess
:return:
"""
sig = signal.SIGKILL # What signal to send
os.killpg(os.getpgid(self.kismet.pid), sig) # Kill one of them
p_list = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE) # Get All processes on system
out, err = p_list.communicate()
for line in out.splitlines(): # For each line (or process)
if 'kismet' in line: # if 'kismet' appears in its name
pid = int(line.split(None, 1)[0]) # Get the process ID
self.logger.debug("Found: %d", pid)
os.killpg(os.getpgid(pid), sig) # Kill the process
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.