Created
October 17, 2019 01:59
-
-
Save apizz/ba526dcc1a314f1c772e70d504affec6 to your computer and use it in GitHub Desktop.
For triggering a restart if more than 1 loginwindow process is detected that is not the current logged in user or root after a user logs out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Determine loginwindow processes running and reboot the machine if | |
# more than 1 is detected. | |
USER=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | /usr/bin/awk -F': ' '/[[:space:]]+Name[[:space:]]:/ { if ( $2 != "loginwindow" ) { print $2 }}') | |
# Searches for loginwindow processes and calculates the number running | |
LOGIN_PROCESSES=$(/usr/bin/pgrep loginwindow) | |
COUNTLOGINS=$(/bin/echo "$LOGIN_PROCESSES" | /usr/bin/wc -l | /usr/bin/sed 's/ //g') | |
LOG="/Library/Logs/ForceReboot.log" | |
P_OWNER=() | |
writelog () { | |
/bin/echo "${1}" | |
/bin/echo $(/bin/date) "${1}" >> $LOG | |
} | |
# If number of loginwindow process is greater than 1, runs the script | |
if [ "$COUNTLOGINS" -gt "1" ]; then | |
writelog "Multiple logins detected. Running script ..." | |
else | |
writelog "Only 1 user detected. Exiting." | |
exit 0 | |
fi | |
for ((i = 0; i < ${#LOGIN_PROCESSES[@]}; i++)); do | |
# Get the loginwindow process owner given the pid and write to array | |
P_OWNER+=($(/bin/ps -eo user,pid | /usr/bin/grep -w "${LOGIN_PROCESSES[$i]}" | /usr/bin/awk '{print $1}')) | |
done | |
for ((i = 0; i < ${#LOGIN_PROCESSES[@]}; i++)); do | |
# If process owner is not the current active user OR root, reboot the machine | |
if [ "${P_OWNER[$i]}" != "$USER" ] && [ "${P_OWNER[$i]}" != "root" ]; then | |
# Force reboot machine | |
writelog "Rebooting to clear logged in user ${P_OWNER[$i]} ..." | |
/sbin/shutdown -r now | |
fi | |
done | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment