Posted on 02-15-2014 07:55 AM
I have a feeling i may be over thinking this, but i can't seem to do what i want. I basically want a nice and friendly way to ask the user to restart after a policy that requires a restart runs. I really don't care for the way JAMF implements restarts, so i'm using a cocoaDialog msgbox to run after the policy runs with the option to restart now or later. I could program the restart button to just 'shutdown -r now', but i'd rather do a friendlier restart like "osascript -e 'tell app "System Events" to restart'". The latter works fine when run from Self Service, but when run from the "Recurring check in" trigger, it fails with an error "28:35: execution error: An error of type -10810 has occurred. (-10810)" which i believe is related to running as the root user, but then why does it work from Self Service?
Any suggestions would be greatly appreciated. Here's the code i'm using:
#!/bin/bash
CD="/opt/local/CocoaDialog.app/Contents/MacOS/CocoaDialog"
rv=`$CD msgbox --title "Restart Computer Please" --text "$4 has been installed and requires a restart"
--informative-text "Click Restart to restart now. Click Later to restart at a later time."
--no-newline --button1 "Restart" --button2 "Later" --float`
if [ "$rv" == "1" ]; then
echo "User selected Restart"
osascript -e 'tell app "System Events" to restart'
elif [ "$rv" == "2" ]; then
echo "User selected Later"
exit
fi
Posted on 02-15-2014 08:57 AM
I run into this kind of stuff frequently it seems these days. You can try adding the following lines to your script. I find myself needing to use this more and more, especially under Mavericks.
loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
loggedInPID=$( ps -axj | awk "/^$loggedInUser/ && /Dock.app/ {print $2;exit}" )
***<cocoadialog line goes here>***
if [ "$rv" == "1" ]; then
echo "User selected Restart"
/bin/launchctl bsexec "${loggedInPID}" sudo -iu "${loggedInUser}" "/usr/bin/osascript -e 'tell app "System Events" to restart'"
***<remainder of script goes here>***
Posted on 02-15-2014 01:01 PM
@mm2270 Thanks. It seems to work when run as root in terminal, but still having trouble with it as a check in policy. I'm going to keep playing with it tho. Also, i may be getting greedy here, but i think the nicer way to do the restart would be to ask for confirmation first. The following code run from terminal as the logged in user works nicely:
#!/bin/bash
osascript -e 'tell application "loginwindow"
«event aevtrrst»
end tell'
Posted on 02-15-2014 02:42 PM
I'm not exactly sure why, but the following code work with the "Recurring Check-in". I think the only thing i changed was having it use the aevtrrst instead of plain restart.
#!/bin/bash
CD="/opt/local/CocoaDialog.app/Contents/MacOS/CocoaDialog"
rv=`$CD msgbox --title "Restart Computer Please" --text "$4 has been installed on your computer and requires a restart"
--informative-text "Click Restart to restart now. Click Later to restart at a later time."
--no-newline --button1 "Restart" --button2 "Later" --float`
if [ "$rv" == "1" ]; then
echo "User selected Restart"
osascript -e 'tell application "loginwindow"
«event aevtrrst»
end tell'
elif [ "$rv" == "2" ]; then
echo "User selected Later"
exit
fi
Posted on 02-17-2014 06:54 AM
This problem is driving me a bit nuts. I thought i had it squared away with the last change, but then i started to have that -10810 error again. I manually restarted my Mac and let the Recurring Check-in run the script again and it started to work again. So, i'm thoroughly confused. At any rate, i made a modification to the script so that it doesn't run at the login window and shortened the osascript to one line. I'll just leave it at that. Not sure if it will be reliable or not.
#!/bin/bash
CD="/opt/local/CocoaDialog.app/Contents/MacOS/CocoaDialog"
loggedInUser=$(ls -l /dev/console|cut -d' ' -f4)
if [ "$loggedInUser" != 'root' ]; then # Check if at the login window, if it is, the following code is skipped
#### CocoaDialog message box asking for the user to select restart
rv=`$CD msgbox --title "Restart Please"
--text "$4 has been installed and requires a restart"
--informative-text "It is important that you select Restart, but you may select Later to restart at a more convenient time."
--no-newline --button1 "Restart" --button2 "Later" --float`
#### If the user selects restart, an apple script runs to run the Apple restart command. Gives the user a minute to cancel restart.
if [ "$rv" == "1" ]; then
echo "User selected Restart"
osascript -e 'tell application "loginwindow" to «event aevtrrst»'
elif [ "$rv" == "2" ]; then
echo "User selected Later"
exit
fi
else
echo "No User logged in. CocoaDialog not necessary"
fi