Search code examples
pythonswiftsubprocesskill

Swift app and python code fail to kill a certain process on mac


I would imagine there are many different possible solutions to this very leveled problem. I am trying to make a swift-based mac app that can manage all my discord bots from one window. I have gotten it to turn on the discord bots successfully(using a global thread, NOT process objects). However, when I quit the app, I noticed the Python process launched by the app keeps running, and so does the discord bot. Instead of the app killing all child processes, it switches the parent process of the python to null when quit. I don't know swift very well, so I had some trouble getting it to kill all child processes when it closes(and yes, I know there is something with info.plist, but that is only for newer XCode versions than I can install). To fix this, I made the applicationWillTerminate code for AppDelegate.swift execute some python code to kill any process that mentions the files of the one bot that the app works with right now. The bot is stored in a folder called roleManager. Here's the python code:

import os
import subprocess
import re
subprocess = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)

output, error = subprocess.communicate()
print(output)
roleProcesses=re.findall("roleManager.{20}", str(output))
#this regex probably could have been better but it works
PIDs=[i.split('\\n')[1] for i in roleProcesses]
for pid in PIDs:
    with open('killProcesses.sh','w') as file:
        file.write(f'kill {int(pid)}')
    os.system('sudo /Users/nathanwolf/Documents/coding/PycharmProjects/botManagerPythonSide/killProcesses.sh')

subprocess.communicate() returns a bytes object with a list of processes formatted like so(I'm pretty sure about this):

CPU time command associated with process(like usr/local/bin/python3.9 some/python/file)\n(not an enter character actually \n)PID ??

The sudo is only there because one time it said it didn't have permission to kill one of the running bots. This approach has had 2 problems. One time it killed its own python process despite not being in the folder roleManager and crashed PyCharm, most of the time it fails to kill the bot. For debugging, I looked for the bot's PID in subprocess.communicate() and it was associated with the following command:

/Library/Developer/PrivateFrameworks/CoreSimulator.framework/Versions/A/XPCServices/SimulatorTrampoline.xpc/Contents/MacOS/SimulatorTrampoline.

The way I see it, there are two pathways to a possible solution: get swift to kill child processes(not sure why that isn't a default), or get python to successfully kill the bot(is the above process related to this?). I would prefer the first one, but either one is fine.

Let me know if you need more info.

Thanks so much in advance!


Solution

  • Found the solution from another stackoverflow question. I just had to execute the following terminal command: pgrep -f Python | xargs kill -9. This kills all running Python apps, which are all going to be controlled from the app, so that works as a patch for me.