How to send signal using kill in MacOS Swift application?

Viewed 631

I have an XCode MacOS application project in Swift. I need to send a signal to another process. Calling of the kill(pid_t, Int32) function doesn't work. My process-receiver doesn't receive any signal. Also, I tried to call bash code from swift using Process:

let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = ["kill", "-s", "SIGUSR1", receiverID]
task.launch()
task.waitUntilExit()
return task.terminationStatus

But I got an error in console kill: 6340: Operation not permitted.

Could you help me? How can I send a signal to another process?

1 Answers

Elevation of privileges is covered in: Apple's security Access Control documentation

However, if you're spawning the original sub-process from your application using NSTask (Process in Swift, thank's Apple!), it should be a child and you should be able to kill it. If you run it using NSTask to spawn it, you should be able to take the returned Process instance and call the terminate() method in order to kill it off.

Thus, if you were to spawn your original child using code similar to your original post (resulting in a Process instance called task), you should be able to call task.terminate() to terminate the process.

Related