Skip to content

Instantly share code, notes, and snippets.

@codeskipper
Last active March 13, 2021 17:29
Show Gist options
  • Save codeskipper/23a1f46b6228fd2cb09d1777c0f52408 to your computer and use it in GitHub Desktop.
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)
#!/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