Getting started with launchctl can definitely be a frustrating experience. I found a lot of articles explaining what you should do but few little downloadable samples. Here is a simple LaunchDaemon that will hopefully be a good starting point. You can just download the files here if you don't feel like copying and pasting.
Note: you need to replace MY_USER_NAME with your username. The plist needs to find your script.
// at ~/Desktop/testdaemon/com.wintr.eodemail.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.wintr.eodemail</string> <key>Program</key> <string>/Users/MY_USER_NAME/Desktop/testdaemon/testdaemon.sh</string> <key>StandardErrorPath</key> <string>/var/log/eod-email.log</string> <key>StandardOutPath</key> <string>/var/log/eod-email.log</string> <key>RunAtLoad</key> <true/> <key>StartInterval</key> <integer>15</integer> </dict> </plist>
This is a simple daemon script that will append the datetime to a file on your desktop. Note: since the script is run as root, tilde (~) won't be the home directory you expect.
// at ~/Desktop/testdaemon/testdaemon.sh #!/bin/sh home="/Users/MYUSERNAME" ## note -- this will be run as root, ~ is not your normal user now=$(date "+%Y-%m-%d %H.%M.%S") echo $now >> "$home/Desktop/TestFile.txt"
Finally, I always write a little shell script to install the LaunchDaemons since it's easy to make a mistake. Since launchctl runs your script as root it requires that script's permissions not be writeable by others, since that would essentially give them root privileges.
// ~/Desktop/testdaemon/install.sh #!/bin/sh -e plist_path="com.wintr.eodemail.plist" plist_filename=$(basename "$plist_path") install_path="/Library/LaunchDaemons/$plist_filename" echo "installing launchctl plist: $plist_path --> $install_path" sudo cp -f "$plist_path" "$install_path" sudo chown root "$install_path" sudo chmod 644 "$install_path" sudo launchctl unload "$install_path" sudo launchctl load "$install_path" echo "to check if it's running, run this command: sudo launchctl list | grep wintr" echo "to uninstall, run this command: sudo launchctl unload \"$install_path\""