简体   繁体   English

如何在 MacOS Swift 应用程序中使用 kill 发送信号?

[英]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.因此,如果您要使用与原始帖子类似的代码生成原始子项(产生一个名为taskProcess实例),您应该能够调用task.terminate()来终止该进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Swift将文档拖到macOS中的应用程序 - Implement document drag to application in macOS using swift Swift macOS:如何粘贴到另一个应用程序中 - Swift macOS: How to paste into another application macOS Swift应用无法通过退出1信号0进行编译 - macOS Swift Apps uncompilable with exit 1 signal 0 iOS SWIFT应用程序 - 如何全局忽略SIGPIPE信号? - iOS SWIFT application - How to ignore SIGPIPE signal globally? macOS Swift:如何将应用程序正确添加为登录项 - macOS Swift: How to properly add application as Login Item 如何在Swift 4 for MacOS应用程序中以编程方式显示窗口/视图控制器 - How to programmatically show a window/view controller in Swift 4 for macOS application 无法在 macOS 11.6 (Big Sur) 上的 swift gui 应用程序中发送击键 - “命令行工具”有效 - Cannot send a keystroke in swift gui application on macOS 11.6 (Big Sur) - "Command Line Tool" works 在 macOS 上使用 Swift 检查可用的 macOS 更新 - Check macOS update available using Swift on macOS 使用 NSuserdefaults 在 Swift 中发出信号 SIGABRT - Signal SIGABRT in Swift using NSuserdefaults iOS推送通知:当应用程序被终止或迅速终止时如何存储推送通知有效载荷? - iOS push notification: how to store push notification payload when application is kill or terminate in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM