I replaced gnome-screensaver with xscreensaver, it works great so far, but I can't get it to lock my session when I suspend my laptop.

Any ideas how to do that?

Ubuntu 10.10 Dell Vostro 1310


  • Some other options listed on the Arch Linux wiki.
    – Sparhawk
    Jun 19, 2015 at 10:49

3 Answers 3


Finally I found the right way of doing this combining the parts I found in /etc/acpi/sleep.sh (that seems left unused there), and Scaine pointed in /usr/lib/pm-utils...

So the final script that works for me is: /usr/lib/pm-utils/sleep.d/00xscreensaver


# Lock xscreensaver on resume from a suspend.

# getXuser gets the X user belonging to the display in $displaynum.
# If you want the foreground X user, use getXconsole!
getXuser() {
        user=`pinky -fw | awk '{ if ($2 == ":'$displaynum'" || $(NF) == ":'$displaynum'" ) { print $1; exit; } }'`
        if [ x"$user" = x"" ]; then
                startx=`pgrep -n startx`
                if [ x"$startx" != x"" ]; then
                        user=`ps -o user --no-headers $startx`
        if [ x"$user" != x"" ]; then
                userhome=`getent passwd $user | cut -d: -f6`
                export XAUTHORITY=$userhome/.Xauthority
                export XAUTHORITY=""
        export XUSER=$user

if pidof xscreensaver > /dev/null; then
    for x in /tmp/.X11-unix/*; do
        displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
        if [ x"$XAUTHORITY" != x"" ]; then
            export DISPLAY=":$displaynum"
            case "$1" in
                    sudo -u $XUSER xscreensaver-command -unthrottle
                    sudo -u $XUSER xscreensaver-command -throttle
                    sudo -u $XUSER xscreensaver-command -lock

The actual xscreensaver commands can also invoked trough su (instead of sudo) like

su $XUSER -c "(xscreensaver-command -lock)"

but that won't works for me because I use pam_mount to mount my encrypted home during login, and pam asks for my password for pam_mount in the script when I use su...


It should be noted that the above answers have been out of date since the introduction of systemd. Now, to lock xscreensaver, one has to either move and modify the helper script for systemd-sleep(8) calling conventions, or use a helper program like xss-lock.

Cf. https://unix.stackexchange.com/questions/149959/how-to-run-systemd-user-service-to-trigger-on-sleep-aka-suspend-hibernate


I'm no expert, but I think you could edit /usr/lib/pm-utils/sleep.d/00powersave to add the xscreensaver-command --lock command at the start.

This will, however, execute twice - once when you suspend, then again when you resume.

Edit : This is a little more involved (not much), but from this thread, it appears that you're better off creating your file in /usr/lib/pm-utils/sleep.d with the command you want to run. So in full, here's the commands from the terminal :

sudo gedit /usr/lib/pm-utils/sleep.d/000LockScreensaver

Then put this in the file and save :


# Lock xscreensaver on resume from a suspend.

case "$1" in resume|thaw)
    xscreensaver-command --lock ;

And finally,

sudo chmod +x /usr/lib/pm-utils/sleep.d/000LockScreensaver

Then try a suspend to see if it works when you resume.

  • Thanks! This pointed me on the right direction but you missed the point that xscreensaver-command needs to access the X server to send the command to xscreensaver daemon... but I posted my script that I finally hacked up :)
    – u-foka
    Jan 9, 2011 at 1:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.