MacOS – How to write a launchd service that will actually stop the service

launchdmacosplist

So I have a plist to keep monit running:

<?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.monit.service</string>
    <key>KeepAlive</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/Cellar/monit/5.10/bin/monit</string>
        <string>-d 60</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/var/log/monit.log</string>
</dict>
</plist>

I can do launchctl load /Library/LaunchDaemons/com.monit.service.plist to start the service, but launchctl unload /Library/LaunchDaemons/com.monit.service.plist will not stop it. I can see that the process keeps running no matter what I do.

The process shows up like this(note how the binary path doesn't show up, only the last argument):

output of ps aux:

root            14656   0.0  0.0  2482248   1656   ??  S     6:55PM   0:00.09 -d 60

How can I write the launchd plist so that it will actually stop the freaking process whenever I want it to stop?

Best Answer

Get rid of the -d 60 arguments and use the launchd key StartInterval instead. Also make sure the process does not daemonize itself. According to the monit man page this is accomplished by using the -I option.