Restart if Computer is at Login Window

el2493
Contributor III

Long story short: I'm managing lab Macs and they're always supposed to automatically login to a public account. Over the past few months I've found that some computers would randomly be at the login screen (either because a user tells it to go to the login window or because of problems with logging in/out), and I wanted to make a script to tell a computer to reboot if it's at the login window (which I'd build into a package and scope to recurring check-in).

Right now I have:

#!/bin/bash -v

#get loggedinuser
loggedInUser="$(/bin/ls -la /dev/console | /usr/bin/cut -d " " -f 4)"

#make sure publicaccount logged in
if [[ $loggedInUser != "publicaccount" ]]
then
    echo "publicaccount not logged in, restarting"
    osascript -e 'tell app "System Events" to restart'
    exit 0
else
    echo "publicaccount logged in"
    exit 1
fi

The part where it detects publicaccount is not logged in works fine, but I can't figure out what command I would need to use to have it restart. I don't think "Finder" or "loginwindow" would work as the app that's being told, and I've tried a few different combinations of restart and «event aevtrlgo» but I keep getting vague error messages, most recently "28:35: execution error: An error of type -10810 has occurred. (-10810)"

Any ideas of what command I would need to use to restart at the login window? Would also need to bypass the "Enter administrator password" window.

4 REPLIES 4

justin_smith
New Contributor III

sudo shutdown -r should work

Asnyder
Contributor III

either "sudo shutdown -r now" or "sudo reboot" should work.

mm2270
Legendary Contributor III

I assume you want to do a graceful restart here? Because if you were ok with forcing a reboot, simply using /sbin/shutdown -r now would do an immediate restart, but this isn't the same as choosing "Restart" from a menu or clicking the Restart button at the login window. It's a little more abrupt than the former.

As for the error of type -10810, this is common when trying to run commands as the owner of console from a root context, which would be the case if Jamf Pro is running the policy. You could try using the launchctl asuser trick here, getting the owner of console and it's UID and using that in the command. For example.

loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u $loggedInUser)

/bin/launchctl asuser $loggedInUID sudo -iu $loggedInUser /usr/bin/osascript -e 'tell app "loginwindow" to «event aevtrrst»'

The above should tell the account that owns console to send an Applescript event command to the loginwindow to restart. I haven't tested the above, but give that a try to see if it works. It's possible this may pop up the "Are you sure?" dialog though. You might have to do a really log out command, then a reboot right after to get it to work reliably.

blackholemac
Valued Contributor III

Not saying you should or shouldn't do this. BUT I am going to say that when you do find something that successfully implements a reboot at the login window, make sure that you have some easy means of reversing that or putting that script/policy in abeyance. There's nothing like a field tech walking up and unwittingly logging out of the Mac to legitimately troubleshoot something only to have the Mac reboot.

On the surface, this reminds me of bad April fool jokes on Mac people back in the Classic Mac OS days. I'm sure you have legit reasons though.

Some dope would put the Shut Down Apple Menu Item in Startup Items on a System 7/Mac OS 8 machine on so the machine would keep shutting itself down unless you knew to turn that off with the Extensions Manager.