xlock on pm-suspend

I’ve always preferred that my machines be locked when they wake up from sleep/suspend/hibernate/etc., and this has been a little bit of a fuss to hand-configure on Linux of late. The problem is that the pm-utils suite that almost all distributions use isn’t really well suited to triggering a lock, and not everyone thinks it should be able to. The Ubuntu solution follows the “not in pm-suspend” idea, and predictably adds another (bulky) layer of abstraction, using gnome-power-manager lock the screen and call the suspend scripts separately. Because I don’t always call pm-suspend the same way and don’t want an extra thing running anyway, that isn’t an option for me. So, a solution to run xlock on every invocation of pm-suspend that ACTUALLY WORKS is to add an appropriately named file in /etc/pm/sleep.d, like the following:

22lock

#!/bin/bash
user=`finger| grep -m1 :0 | awk '{print $1}'`
case $1 in
    hibernate)
        su $user -c "xlock -mode blank -display :0&"
        ;;
    suspend)
        su $user -c "xlock -mode blank -display :0&"
        ;;
    thaw)
        ;;
    resume)
        ;;
    *)  echo "The xlock-on-sleep script is broken"
        ;;
esac

Remember to make the file executable (chmod +x).
The finger/grep/awk incantation at the top is a cheap (and not entirely proper) way of grabbing the first user on display :0, which is USUALLY the user logged in on what is USUALLY the local X server; sudoing to an appropriate user (and the explicit “-display :0”) is required because the script is run in an environment where the display isn’t visible and the user is always root.
xlock and it’s options can be modified or swapped out for your screen-locker of choice.

(Posting as a reminder to myself, and because I didn’t see a solution when I searched)

This entry was posted in Computers, DIY, General, OldBlog and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *