Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

launchd Global Daemon StartInterval problem

OSX 10.9 and 10.10 Macs in a corporate environment.

I've got a mechanism in place that is designed to check the uptime of the computer it is run on, and if the uptime is greater than 14 days, it then displays an applescript "nag screen" to ask the user to Shutdown, restart or cancel. If the user clicks cancel on the dialog box I have a StartInterval defined in the .plist to run again every 2 hours (7200 seconds) to prompt them again. It works wonderfully except that when the computer's uptime is in excess of the prescribed number of days and the script runs and the user clicks on Cancel again and again, the StartInterval will work only 2 or 3 more times and then it won't automatically run again. I've found that if I set the StartInterval to an extremely short time like 60 seconds, it will continue to run no matter how many times the user clicks cancel. When I set it to longer than about 5 or 10 minutes, that's when it will only run a couple times before stopping.


Here is my LaunchDaemon plist located in /Library/LaunchDaemons/. As you can see, all it does is run a up.sh script every 2 hours:

<?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>Disabled</key>

<false/>

<key>EnvironmentVariables</key>

<dict>

<key>PATH</key>

<string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/gi t/bin</string>

</dict>

<key>Label</key>

<string>com.company.uptime</string>

<key>Program</key>

<string>/Library/Application Support/company/up.sh</string>

<key>RunAtLoad</key>

<true/>

<key>StartInterval</key>

<integer>7200</integer>

</dict>

</plist>


Here is the content of the up.sh script that checks the uptime and if the uptime is greater than 14 days, then then runs time.sh script :


#!/bin/bash


up="`uptime | sed -n -e 's/.* up \([0-9][0-9]*\) day.*/\1/p'`"

if [ -n "$up" ] && [ "$up" -gt 14 ]

then /Library/Application\ Support/company/time.sh

fi

And here is the content of the time.sh script that displays a prompt with 3 buttons Shut Down, Restart and Cancel. Shut Down and Restart do exactly that. Cancel just makes the window go away ... until the next 2 hour interval when the LaunchAgent runs again:


#!/bin/bash

VARIABLE=$(/usr/bin/osascript <<-EOF

tell application "System Events"

activate

set question to display dialog "This computer has not been shut down or restarted for more than 14 days. Please restart as soon as possible to ensure that all security patches deployed to the computer are applied. This also helps the computer run at optimum performance. This message will be repeated every 2 hours until you restart." with title "company notice: RESTART YOUR COMPUTER" buttons {"Shut Down", "Restart", "Cancel"} with icon file "Blue-AV-Mark.icns"

set answer to button returned of question

if answer is equal to "Shut Down" then

tell application "System Events"

shut down

end tell

end if

if answer is equal to "Restart" then

tell application "System Events"

restart

end tell

end if

if answer is equal to "Cancel" then

return

end if

end tell

EOF)



$VARIABLE

exit 0


Like I said, for very short intervals where the user clicks Cancel, it seems to work just fine, but when I try to set the interval at 7200 seconds it only works a couple times when the user clicks Cancel. If I were to unload and then load the LaunchDaemon again, it will work again for a few times, but obviously that defeats the purpose of this mechanism. Does anyone have any ideas for why the longer StartInterval doesn't seem to work?

Posted on May 12, 2015 7:13 AM

Reply

There are no replies.

launchd Global Daemon StartInterval problem

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.