Last active
March 13, 2021 17:29
-
-
Save codeskipper/23a1f46b6228fd2cb09d1777c0f52408 to your computer and use it in GitHub Desktop.
macOS - bash/python - Check for user presence at console by checking for screen lock and console login status (must run locally by logged in account)
This file contains hidden or 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 | |
# Check for user presence at console by checking for screen lock and console login status | |
# Thanks to https://stackoverflow.com/users/908494/abarnert at | |
# https://stackoverflow.com/a/11511419/4326287 | |
# Wait until console is logged in and screen is unlocked | |
while true ; | |
do | |
timestamp=$(date +%Y-%m-%dT%H:%M:%S%z) | |
python -c 'import sys,Quartz; d=Quartz.CGSessionCopyCurrentDictionary(); sys.exit(d and d.get("CGSSessionScreenIsLocked", 0) == 0 and d.get("kCGSSessionOnConsoleKey", 0) == 1)' | |
if [ $? != 0 ] ; then | |
echo "$timestamp [userPresence.sh] User presence detected" | |
break | |
else | |
echo "$timestamp [userPresence.sh] Screen is locked or no console user is logged in" | |
fi | |
sleep 10 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment