I have a large number of labs in our district and students will either walk away from their workstations at the end of class and rely on another student switch users or just lock their account and leave. Both scenarios leave the student logged in. This can create some issues when attempting to use remote commands so I am looking for a configuration or script that I can deploy that will log users out. I would think the easiest way to do this is deploy something at the end of the day. Just wondering if anyone has something they are using that they like.
I need a a way to automatically log out users
Best answer by PaulHazelden
I run a pair of scripts, they are launched by a pair of Launch Daemons.
One will attempt to force log out any user account on the Mac at a set time, thus allowing my power management settings to shut the Mac down. But this can hit problems when the user leaves unsaved work open.
The second is a bit more draconian. It will shut the Mac down at a set time, regardless of work saved or not.
These are the bits of the script that do the work, I have logging in place so I can check on things later if i need to.
Logout script does this...
CURRUSER=$(/usr/bin/who | /usr/bin/grep console | /usr/bin/head -n 1 | /usr/bin/awk '{print $1}')
# Kill the login session
/bin/kill `ps uaxww | grep $CURRUSER | grep loginwindow | awk '{print $2}'`
The Hard Shutdown script has this added to it after the kill...
shutdown -h now
The LaunchDaemon for the first script is...
<?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>Label</key>
<string>com.UNIQUE.NAME.FOR.DAEMON</string>
<key>ProgramArguments</key>
<array>
<string>/Path/to/script/shutdown.sh</string>
<string>-argument</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>20</integer>
<key>Minute</key>
<integer>55</integer>
</dict>
</dict>
</plist>
And the Hard ShutdownDaemon is...
<?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>Label</key>
<string>com.UNIQUE.NAME.FOR.DAEMON</string>
<key>ProgramArguments</key>
<array>
<string>/PATH/TO/SCRIPT/shutdownhard.sh</string>
<string>-argument</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>21</integer>
<key>Minute</key>
<integer>15</integer>
</dict>
</dict>
</plist>
You will need to put in a Unique name for the LaunchDaemons, and the correct Path to your scripts. You will also note I have the Logout run at 20:30 daily and the Hard shutdown at 21:15, My power settings are set to shutdown at 21:00 daily.
As these are LaunchDaemons they are set to run at every boot, and the Power management settings are used for the most part to shut the Macs down in a nice way.
In addition I do have a loginwindow profile that includes the Log out after 30 mins of inactivity, but this does not always work because they can leave unsaved work open.
As with all scripts, please test, test and test again before putting into production.
Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.