Launchd unloading LaunchDaemon

bashcronlaunchdplist

I have a script scheduled to run as LaunchDaemon. Script runs fine when launched manually through terminal.

I have two commands

sudo launchctl unload -F /Library/LaunchDaemons/com.test.plist
sudo launchctl load -F /Library/LaunchDaemons/com.test.plist

plist 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.test.plist</string>
 <key>Program</key>
 <string>/usr/local/bin/test.sh</string>
 <key>StandardErrorPath</key>
 <string>/tmp/com.test.plist.err</string>
<key>StandardOutPath</key>
<string>/tmp/com.test.plist.out</string>
<key>StartCalendarInterval</key>
<dict>
    <key>Hour</key>
    <integer>18</integer>
    <key>Minute</key>
    <integer>4</integer>
</dict>

when run separately they do work. But once scheduled in launchd it runs for split second them goes to unloaded. I am guessing once it unloads the script it stops running. I change the values of the intervals set in the plist with another script which is why I need to unload and load. Is there another way to do this?

Best Answer

Is the problem perhaps that your unload command hasn't finished before the load command starts?

How about trying kickstart instead? "-k" instructs launchctl to "kill the running instance before restarting the service."

sudo launchctl kickstart -k /Library/LaunchDaemons/com.test.plist

Note that load and unload are now under "Legacy commands" in the Yosemite launchctl man page.