MAC OSX Agents - how to launch multiple instances

▼魔方 西西 提交于 2020-01-15 17:49:30

问题


I am trying to create helper objects (it could be a daemon or an agent). This should be launch on demand and its multiple instances should be there, as you see for Chrome and Safari helpers.

What Have I tried? I have referred these Apple Docs, Launchd Tutorial & Creating Start Up Items in Mac OS X

There it is mentioned that for number of processes we should use NumberOfProcesses key, I used it but in Activity Monitor I see only one instance.

And my plist looks like this

<?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>Label</key>
    <string>com.anoop.test</string>
    <key>NumberOfProcesses</key>
    <string>5</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/anoopvaidya/Desktop/0@/WebTwainService</string>
        <string>start</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>Application is agent (UIElement)</key>
    <string>YES</string>
</dict>
</plist>

Am I on correct path to replicate Launch Agent Helper as of Chrome? Please help me to find the best way to get it done?


回答1:


NumberOfProcesses is not a top-level key. It's a sub-key of HardResourceLimits, and it's used to indicate how many sub-processes your daemon or agent may create before the kernel stops you. It has nothing to do with launchd creating multiple instances of your process.

Launchd does not support managing multiple processes for a single job. Chrome and Safari do not use Launchd to manage their job processes. They manage these themselves.

If you want a separate process per application being helped, then you can achieve that with an Application XPC Service, but XPC also does not support multiple processes for a service that is assisting a single application. See Running multiple instances of the same XPC service (NSXPCConnection) for more.

The typical pattern is to have a single LaunchAgent/Daemon that accepts incoming connections, and then forks itself to handle each client independently. I assume the same pattern should work for XPC Services (which are now preferred), but I haven't built one that way before.



来源:https://stackoverflow.com/questions/29680596/mac-osx-agents-how-to-launch-multiple-instances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!