How to suspend with a keyboard shortcut?

17,464

Solution 1

I wrote a script. It seems to do what you ask for:

#!/usr/bin/env zsh
# Custom suspend
#
# (That 'zsh' up there can be switched to 'bash', or 
# pretty much any shell - this doesn't do anything too fancy.)
#
# Dependencies are mostly xfce stuff:
#
#   xbacklight
#   xflock4
#   xfce4-session-logout

# Set how dim we want the screen to go (percentage, out of 100)
dim=5

# Pack up your toys
previous_dimness=$(xbacklight -get)

# Turn down the lights
xbacklight -set $dim

# Lock the door (this requires a password to get back in)
xflock4

# And go to sleep
xfce4-session-logout --suspend

# When we wake up, turn the lights back on
xbacklight -set $previous_dimness

Solution 2

You can very simply send the command for it to lock screen first, and then have it suspend:

xscreensaver-command -lock ; xfce4-session-logout --suspend

For usage of xscreensaver-command, run xscreensaver-command --help

Solution 3

Oddly enough xfce4-session-logout --suspend works just fine, and my system is xubuntu 14.04. It also does lock the session, although I have switched on the option "lock screen before sleep", go to Settings->Session and Startup-> Advanced, and check if it switched on there.

Share:
17,464
ppr
Author by

ppr

Updated on September 18, 2022

Comments

  • ppr
    ppr almost 2 years

    I would like to suspend my xubuntu (14.04) system from a keyboard shortcut without entering my superuser password (sudo). I'm looking a command line which I can convert to a shortcut.

    So far, I tried two solutions:

    Xfce command:

    xfce4-session-logout --suspend
    

    Problem: The system doesn't lock the session. I don't need to enter my password for the wake-up and I want to do it.

    Dbus:

    dbus-send --print-reply --system --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend
    

    Problem: After wake-up, the Internet connection is down and I have to reboot the system to get it back.

    Is there a third solution which 1. ask the password during the wake-up process, and 2. doesn't mess up with Internet connection?

    In fact, the graphical default shortcut (from the menu) works fine. I just don't know which command line is called.

  • BobDoolittle
    BobDoolittle over 8 years
    That works from command line (although the lock screen is unnecessary since that's automatic with suspend), but not as a keyboard application shortcut (at least with Ubuntu 12.04). By the way, the Xfce app for locking the screen is xflock4 (no args), which by default is bound to control-alt-delete.
  • BobDoolittle
    BobDoolittle over 8 years
    That works from command line (although the lock screen is unnecessary since that's automatic with suspend), but not as a keyboard application shortcut (at least with Ubuntu 12.04).
  • Ufos
    Ufos over 8 years
    well, it is supposed to work in xubuntu, not ubuntu. I am not sure why do you even have the xfce4-session-logout command.
  • BobDoolittle
    BobDoolittle over 8 years
    We are talking Xfce, which can be added to any ubuntu distribution. Xubuntu is simply a convenience distro that happens to have added Xfce packages and configured them as a desktop. You can start with ubuntu and add/configure it yourself. The xubuntu/ubuntu distinction is a red herring here.
  • Wildcard
    Wildcard over 8 years
    +1 for the wonderful comments. I think I will use this script when I install Ubuntu.
  • Jesse Glick
    Jesse Glick over 8 years
    Xubuntu 15.10: Error org.freedesktop.DBus.Error.UnknownMethod: Method "Suspend" with signature "" on interface "org.freedesktop.UPower" doesn't exist
  • Jesse Glick
    Jesse Glick over 8 years
    xscreensaver-command seems to be gone in 15.10.
  • Jesse Glick
    Jesse Glick over 8 years
    Working fine for me, with screen lock, in Xubuntu 15.10.
  • TheBeginner
    TheBeginner over 8 years
    @JesseGlick - just install the xscreensaver package
  • hipoglucido
    hipoglucido almost 7 years
    Im sorry but where should I place this script? how should I connect it with a keyboard shortcut (for example ctrl+alt+s)? I am using Lubuntu
  • Jon Carter
    Jon Carter almost 7 years
    You can technically put the script anywhere. I have a bin/ directory under my home directory just for personal scripts like this that I want to use. In this case, the path to the script is: /home/<username>/bin/custom-suspend You'll want to make sure the script is executable. One way to do that is to run the following command: $ chmod +x /home/<username>/bin/custom-suspend To give it a shortcut under LXDE (which Lubuntu) uses, you should be able to search for something like 'keyboard' from the start menu, which should show you the keyboard config app.