Deploying Launchdaemons

ChristopherGlov
New Contributor III

Hello Everyone, I am making a launch daemon to run a python script at a set interval. For packaging I packaged the daemon with the files it needs and Im inputing a posinstall script to start the daemon. Is this best practice?

4 REPLIES 4

mm2270
Legendary Contributor III

Yes. That's the way I've done it in the past, as long as you're ok with doing this in a pkg.

I'd like to offer an alternative though. It can be done entirely in a script since a LaunchDaemon is nothing but a plist that can be generated from within a script and then activated (after making sure permissions and ownership are correct.) I've had better luck in deploying LaunchDaemons this way more recently.

Here's an example, using generic information. You can use it as a template and just copy/paste over the whole plist section from your actual LaunchDaemon:

#!/bin/bash

cat << EOF > /Library/LaunchDaemons/com.org.mydaemon.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>Label</key>
    <string>com.org.mydaemon</string>
    <key>ProgramArguments</key>
    <array>
        <string>/path/to/script</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
EOF

chmod 644 /Library/LaunchDaemons/com.org.mydaemon.plist
chown root:wheel /Library/LaunchDaemons/com.org.mydaemon.plist

/bin/launchctl load /Library/LaunchDaemons/com.org.mydaemon.plist

EDIT: I just re-read your post and I see you mentioned you "packaged the daemon with the files it needs" which I assume means there are some support files, scripts, etc to go along with it (of course, the python script in question). While you could technically create the python script on the fly from within the bash script above using the same cat << EOF > /path/to/file method, but... at a certain point there are diminishing returns with this process. If you have to create more than just one or two files this way, it's probably easier to package it all up instead in a package and use a postinstall script.

ChristopherGlov
New Contributor III

Thanks a lot for the response. So far the is what I have below. Im having issues getting it to actually run the python script. There is a setting.json script in the directory the script is in that it needs to talk to the web server

<?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>AdminPUM</string>
        <key>Program</key>
        <string>/Library/PUM/localUpdateServicePython.py</string>
        <key>StartInterval</key>
        <integer>14400</integer>
        <true/>
    </dict>

mm2270
Legendary Contributor III

@TheBeastie You're plist is not formed correctly. You can't use

<key>PUM</key>

for the first entry. It must be
<key>Label</key> and the <string>AdminPUM</string> under it is fine as is.

Nevermind the below. I see you fixed it so I assume it was a copy/paste error.
The other issue I see, unless it was a copy/paste error is

<integer>14400</integer?

The ? at the end needs to be a >

ChristopherGlov
New Contributor III

thanks a lot that seems to be the mistake I was overlooking.