Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.


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


share|improve this question
Some other options listed on the Arch Linux wiki. –  Sparhawk Jun 19 at 10:49

2 Answers 2

up vote 3 down vote accepted

Finally I found the right way of doing this combining the parts I found in /etc/acpi/ (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...

share|improve this answer

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.

share|improve this answer
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 '11 at 1:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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