Panda
Panda

Reputation: 159

Subprocess.call() won't run kill all, I'm on macOS using python

I am trying to kill an app using killall on macos, but everytime I try to do it, it doesn't kill the app. I have the right name and all but it still won't kill the app.

My code:

def KillApp(appName):
    
    """

    This will close a program if the app is open.

    """
    os_name = system()
    
    if os_name == "Darwin":
        
        os_name = "macOS"
    
    if system() == "Windows":
        
        return call(["taskkill", "/f", "/im", appName], shell=True)
    
    elif system() == "Linux":
        
        return call(["killall", appName], shell=True)
    
    elif system() == "macOS":
        
        return call(["killall", appName], shell=True)`

It returns None and doesn't kill the program.

Upvotes: 1

Views: 95

Answers (1)

Maurice Meyer
Maurice Meyer

Reputation: 18136

subprocess.call() uses the same function signature as the Popen constructor.
The command executed via Popen needs to be passed in different ways depending on shell argument:

  • shell=True, the command needs to be a string.
  • shell=False,the command needs to be a list.

Examples:

return call(["killall", "-9", appName], shell=False)
return call(f"killall -9 {appName}", shell=True)

Upvotes: 2

Related Questions