Skip to content

Instantly share code, notes, and snippets.

@anroots
Created May 3, 2016 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anroots/410b5f3b59a0d1fbe2db84b1332c60af to your computer and use it in GitHub Desktop.
Save anroots/410b5f3b59a0d1fbe2db84b1332c60af to your computer and use it in GitHub Desktop.
Cycle the screen through "locked" and "unlocked" states while leaving user input blocked
#!/bin/bash
# Script to control screen lock
#
# Dependencies: xtrlock, xscreensaver and xscreensaver-command
#
# Use case: you want the cycle the screen between ON (all windows visible)
# and OFF (blank screen saver, password needed to unlock) states while disabling user input
# when screen is ON. Great for kiosk PC-s to turn the screen off during the night.
#
# The screen can either be "locked" or "unlocked"
# - locked: screen is "blank" (screensaver), needs a password to unlock
# - unlocked: everything on the screen can be seen, but no user input is allowed (xtrlock)
#
# Usage Example:
# These cron entries lock the screen after the workday ends and unlock it at 9am.
# cron: name="lock-screen" minute="0" hour="09" weekday="1-5" job="/usr/local/bin/lock-control --lock" user=kiosk
# cron: name="unlock-screen" minute="0" hour="18" weekday="1-5" job="/usr/local/bin/lock-control --unlock" user=kiosk
#
# Author Ando Roots <ando@sqroot.eu> 2016
function usage
{
echo "usage: lock-control.sh [-l] | [-u] | [-h]"
}
function lock
{
echo "Locking screen..."
# Kill xtrlock if it's running
if pidof "xtrlock" > /dev/null
then
killall xtrlock
sleep 1
fi
# Start xscreensaver if it's not running
if ! pidof "xscreensaver" > /dev/null
then
echo "Starting xscreensaver..."
DISPLAY=:0 xscreensaver -nosplash &
sleep 1
fi
# Lock screen
DISPLAY=:0 xscreensaver-command -lock
}
function unlock
{
echo "Unlocking screen..."
# Stop the screensaver
if pidof "xscreensaver" > /dev/null
then
killall xscreensaver
sleep 1
fi
# Disable user input
DISPLAY=:0 xtrlock &
}
while [ "$1" != "" ]; do
case $1 in
-l | --lock ) shift
lock
;;
-u | --unlock ) unlock
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment