MacOS – on-demand launch agent restarting automatically

daemonslaunchdmacos

As title says I have created a on-demand launch agent that listens to socket and is launched on incoming connection. It works well and after it has done it's job I want to quit it. After quitting system automatically restarts it, OS probably thinks it died accidentally. How can I stop this behaviour? My launch agent is a Cocoa application (without GUI), I have tried quitting with 'exit(EXIT_SUCCESS)' and '[[NSApplication sharedApplication] terminate:self]'

My launchd plist is below:

<?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.mycompany.mylaunchagent</string>
<key>ProgramArguments</key>
<array>
    <string>/Library/Application Support/com.mycompany.myproduct/myproduct.app/Contents/MacOS/myproduct</string>
</array>
<key>ServiceIPC</key>
<true/>
<key>KeepAlive</key>
<false/>
<key>Sockets</key>
<dict>
    <key>MyListenerSocket</key>
    <dict>
        <key>SockServiceName</key>
        <string>12040</string>
    </dict>
</dict>

Best Answer

It probably won't help, but you could try sleeping for a few seconds before exiting. From Daemons and Services Programming Guide:

Important: If your daemon shuts down too quickly after being launched, launchd may think it has crashed. Daemons that continue this behavior may be suspended and not launched again when future requests arrive. To avoid this behavior, do not shut down for at least 10 seconds after launch.