my 2 cents: You succeed in killing "iTunes" only if You Xcode App will run with SandoBox DISABLEDenter image description here

All examples on stack overflow about Process are misleading as they call "ls" or "echo" that are always executed in system folders.


I'd suggest you first read the man page for sysctl -- it's used to get and set kernel state. Does that sound like something you want?

The path to killall is /usr/bin/killall, which you can find from Terminal:

> which killall
/usr/bin/killall

Here's the full Swift code:

let pipe = Pipe()

let task = Process()
task.launchPath = "/usr/bin/killall"
task.arguments = ["iTunes"]
task.standardOutput = pipe
task.standardError = pipe
task.launch()
task.waitUntilExit()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = String(data: data, encoding: .utf8) {
    print(output)
}