macOS上的Swift:后台应用终止时运行代码

问题描述 投票:0回答:1

我正在编写一个Mac应用程序,该程序在后台作为守护程序运行。如何检测应用程序何时被杀死并运行一些清除代码?我曾尝试捕获SIGTERMSIGINTSIGKILL之类的信号,但是我的应用程序结构似乎对此有一些问题。

这是我的应用程序从主线程开始的基本流程:

func signalHandler(signal: Int32) { ... }

let signals = [SIGINT, SIGTERM, SIGKILL]
for sig in signals {
    signal(sig, signalHandler)
}

// code to kick off background execution that will occasionally need to run some stuff on the main thread

RunLoop.current.run() // SIGTERM shows up in Xcode on this line, signalHandler not called

[如果我使用Xcode的停止按钮停止应用程序或从活动监视器强制退出,则该应用程序将以退出代码9(SIGKILL)退出。如果我从活动监视器中选择“退出”,则应用程序会收到SIGTERM,但不会调用处理程序... Xcode仅突出显示RunLoop行并说Thread 1: signal SIGTERM

没有RunLoop行,我的应用程序几乎立即停止,但是我需要保持它的生命。同时,我在想这行可能会阻塞主线程并阻止信号处理程序运行。有没有什么办法可以解决我的应用终止问题?

swift macos kill-process sigint sigterm
1个回答
1
投票

一些注意事项:

  • [SIGKILL cannot be caught
  • 不要运行带有lldb(Xcode)的程序,否则会与信号处理程序混在一起。

尝试打印信号并从命令行启动程序


func signalHandler(signal: Int32) {
    print("Caught signal \(signal)")
}

Ctrl + C打印Caught signal 2

通过Quit(不是Force Quit)从活动监视器终止过程将打印Caught signal 15

© www.soinside.com 2019 - 2024. All rights reserved.