I created a script to restart a computer after 6 hours idle (by cobbling together stuff I'd found online):
#!/bin/sh
exec 2>&1
# Get MacOSX idletime. Shamelessly stolen from http://bit.ly/yVhc5H
idleTime=$(/usr/sbin/ioreg -c IOHIDSystem | /usr/bin/awk '/HIDIdleTime/ {print int($NF/1000000000); exit}')
echo Idle Time is $idleTime seconds
if (( idleTime > 21600 )); then
sudo shutdown -r now
else exit 0
fi
exit 0
That works, but it doesn't seem to trigger logout hooks. We use Jamf to manage lab computers and if we logout the lab account it will automatically restart the computer, so logging out the lab account would technically accomplish the same thing as restarting. If I type the following command into terminal, it immediately brings up a popup window saying it will logout in 60 seconds and then logs out:
osascript -e 'tell app "System Events" to log out'
But if I put that into my script (replacing "sudo shutdown -r now"), I get the error message "28:35: execution error: An error of type -10810 has occurred. (-10810)<br/>"
Any ideas? I know there is a Configuration Profile to logout users after certain amount of time, but it hasn't worked consistently for me so I'm trying to make an alternative.
