Skip to main content

Goal: I want to have a script that will restart a system, if no user is logged in, or prompt a logged in user that their computer is going to be restarted in 30 minutes, and give them an option to postpone that restart.



I found the following script ( https://jamfnation.jamfsoftware.com/viewProductFile.html?fid=477 ), and it appears to work, except it always restarts after running for just 5 minutes.



If I copy the large chunk of AppleScript code into Script Editor, and run it there, it works fine for the full duration (30 minutes).



I have tried adding some pieces like "with timeout of 1800 seconds", but i get the same result. I have also tried on multiple OS's (10.11-10.9).



Could anyone clue me in on why the Applescript is acting differently when run from inside a .sh script?

Are you runnning it as a policy? You haven't set a restart condition on the policy itself? That defaults to 5 minutes I believe...


Negative, I have tried running this script from terminal to rule out anything like that.


Do a "with timeout of #### seconds" after the activate line. Here is an example from one of my scripts where I set the time-out slightly longer than the longest I possibly want the script to run. Then the timeouts within will have time to work.



    tell application "Finder"
activate
with timeout of 86400 seconds

Because it's a Friday before a long weekend and I'm trying to avoid my own work, I created this template. Hopefully it helps you- or at least parts of it.



Run the script below frequently on your machines. It looks for a key in /tmp/rebootTrack. If that key exists, the user is prompted to restart, and it does reboot automatically with a 30 minute (1801 second) countdown if someone is logged in. They can delay the reboot 5 times. Run the script again to prompt the user again. Either with LaunchD or Policy.



#!/bin/bash                               
#Thoule

JH="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
JHaddon=" -windowType utility -title IT_Message -icon /usr/local/Logos/Square_Black.png "

needReboot=$(defaults read /tmp/rebootTrack rebootNeeded)
if [ "$needReboot" != "YES" ]; then
echo "no reboot required at this time"
exit 1
fi

currentUser=$(stat -f '%Su' /dev/console)

snoozeCount=$(defaults read /tmp/rebootTrack snoozeLeft)
if [ -z $snoozeCount ]; then #if no value returned
snoozeCount=5 #get five snoozes by default.
defaults write /tmp/rebootTrack snoozeLeft -int 5
fi

alreadyRunning=$(defaults read /tmp/rebootTrack runningNow)
if [ -z $alreadyRunning ]; then
defaults write /tmp/rebootTrack runningNow -string "running"
else
echo "another instance of this script is running. I'll quit now."
exit 1
fi

if [ "$currentUser" == "root" ]; then
echo "rebooting here"
/sbin/reboot
else
"$JH" $JHaddon -description "Reboot Pending. Get yerself ready! You have $snoozeCount delays left" -timeout 1801 -button2 "Delay" -button1 "Reboot" -countdown
response=$?

if [ "$response" == "0" ] || [ $snoozeCount < 1 ]; then #if reboot clicked
echo "rebooting Here!"
/sbin/reboot
else
snoozeCount=$((snoozeCount-1))
defaults write /tmp/rebootTrack snoozeLeft $snoozeCount
fi
fi

#cleanup
defaults delete /tmp/rebootTrack runningNow

I tried the 'with timeout' option also, but it still would not work as expected. So I have just dumped this and written my own python script using cocoa dialog that does what I want.



Thanks for the suggestions though.


Just chiming in, never had luck with the timeout flag for anything more than 5 minutes.



CocoaDialog or (if an app), building a XIB works. (Had to do this in AutoCasperNBI).