[英]How to send signal using kill in MacOS Swift application?
I have an XCode MacOS application project in Swift.我在 Swift 中有一个 XCode MacOS 应用程序项目。 I need to send a signal to another process.我需要向另一个进程发送信号。 Calling of the kill(pid_t, Int32)
function doesn't work.调用kill(pid_t, Int32)
function 不起作用。 My process-receiver doesn't receive any signal.我的进程接收器没有收到任何信号。 Also, I tried to call bash code from swift using Process
:另外,我尝试使用Process
从 swift 调用 bash 代码:
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
.但是我在控制台kill: 6340: Operation not permitted
中遇到错误。
Could you help me?你可以帮帮我吗? How can I send a signal to another process?如何向另一个进程发送信号?
Elevation of privileges is covered in: Apple's security Access Control documentation权限提升包含在: Apple 的安全访问控制文档中
However, if you're spawning the original sub-process from your application using NSTask
( Process
in Swift, thank's Apple,).但是,如果您使用NSTask
从应用程序中生成原始子流程(Swift 中的Process
,感谢 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.如果您使用NSTask
运行它来生成它,您应该能够获取返回的Process
实例并调用terminate()
方法以将其杀死。
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.因此,如果您要使用与原始帖子类似的代码生成原始子项(产生一个名为task
的Process
实例),您应该能够调用task.terminate()
来终止该进程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.