【问题标题】:How to keep a macOS Launch Agent open until the app that requested the service is closed?如何在请求服务的应用程序关闭之前保持 macOS 启动代理打开?
【发布时间】:2022-10-12 22:17:19
【问题描述】:

使用XPC 处理我的第一个 macOS 启动代理。

我需要提供服务的进程由launchd 启动,然后保持运行直到发出初始调用的客户端进程关闭。

我在启动代理的plist 中将KeepAlive 设置为true,但显然这意味着即使在进行初始调用的客户端进程结束后,该进程仍然存在。

在launchd.plist(5) 的文档中,它指出“可以指定条件字典来选择性地控制是否启动 让工作保持活力”......有人知道这里指的是什么字典以及如何实施这些条件吗?

编辑

为上下文添加代码,尽管我会强调这一切都可以正常工作并且行为符合我的预期(设置连接以启动服务)。

感谢 rderik 提供示例代码 (https://github.com/rderik/rdConsoleSequencer)。

// Connecting to the service from the client...
        let connection = NSXPCConnection(machServiceName: "com.name.servicename")
        
        connection.remoteObjectInterface = NSXPCInterface(with: MyXPCProtocol.self)
        connection.resume()
    
        let service = connection.remoteObjectProxyWithErrorHandler { error in
            print("Received error:", error)
        } as? MyXPCProtocol


// Service main.swift ...
let listener = NSXPCListener(machServiceName: 
"com.name.servicename")
let delegate = ServiceDelegate()
listener.delegate = delegate;
listener.resume()
RunLoop.main.run()

// Service class...
@objc class MyXPC: NSObject, MyXPCProtocol {
    
  // My service functions... 
    
}

// Service delegate...
class ServiceDelegate: NSObject, NSXPCListenerDelegate {
    func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
        let exportedObject = MyXPC()
        newConnection.exportedInterface = NSXPCInterface(with: MyXPCProtocol.self)
        newConnection.exportedObject = exportedObject
        newConnection.resume()
        
        return true
    }
}

// Service protocol
@objc(MyXPCProtocol) protocol MyXPCProtocol {
    
    // My protocol functions...
}


// User LaunchAgents plist...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
  <string>com.name.MyXPC</string>
  <key>Program</key>
  <string>/mypath.../</string>
    <key>MachServices</key>
    <dict>
        <key>com.name.myservice</key>
        <true/>
    </dict>
</dict>
</plist>

【问题讨论】:

  • 请单击edit 并添加足够的代码和您的plist 文件,以便人们为您提供帮助。

标签: swift macos launchd xpc launch-agent


【解决方案1】:

感谢 rderik 对此的帮助。

这里的一种方法是使用 PathState 来评估进程是否应该保持运行。您可以在应用程序启动时创建一个文件,该文件的路径与启动代理中的键对应,其中键与 PathState 相关联。如果 PathState 设置为 true 并启动 Launch Agent 进程,它将保持运行。

然后,当客户端应用程序进程结束时,删除文件,启动代理进程现在也可以结束。

我的实现中仍然缺少一些东西,因为当客户端应用程序关闭时启动代理进程似乎不会自动结束 - 如果您知道如何解决这个问题,请添加评论。

示例启动代理 plist 包括 PathState 键:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>KeepAlive</key>
    <dict>
    <key>PathState</key>
    <dict>
        <key>/path_to_temp_file</key>
            <true/>
    </dict>
    </dict>
  <key>Label</key>
  <string>com.name.MyXPC</string>
  <key>Program</key>
  <string>/path_to_service/</string>
    <key>MachServices</key>
    <dict>
        <key>com.name.servicename</key>
        <true/>
    </dict>
</dict>
</plist>

在应用程序生命周期的开始和结束时创建和删除文件:

@main
class AppDelegate: NSObject, NSApplicationDelegate {

    
    let fileManager = FileManager.default
    let path: String = "/path_to_temp_file"
    let contents = Data()
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        
        fileManager.createFile(atPath: path, contents: contents, attributes: [:])
    }

    func applicationWillTerminate(_ aNotification: Notification) {

        let urlPath = URL(fileURLWithPath: path)

        do {
            try fileManager.removeItem(at: urlPath)
        } catch {
            print("Error removing serviceProcessTemp file: (error)")
        }
    }
}

笔记:

我要补充一点,弄乱 plist 结构会导致错误,我发现使用 plutil 检查修改后的 plist 文件是否正确很有用......

$ plutil -lint /path/to/propertylist.plist

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2021-08-11
    • 1970-01-01
    相关资源
    最近更新 更多