Ask Your Question

Is there a way to lock the gnome session and suspend the computer with a single keyboard shortcut?

asked 2015-05-29 17:44:47 -0500

mightyflea gravatar image

Hi all

I am looking for a way to lock my gnome session and suspend the computer with a keyboard shortcut.

The background: I do not want the laptop to go to sleep when I close the lid, so I had to disable the lid switch in the logind.conf file.

I do not want the screen to lock when the computer just goes idle, I lock it manually whenever I need to. So I set the screensaver to not lock the screen when the screen saver kicks in.

I wanted a convenient method to suspend the computer at a keypress, so I created a custom shortcut in gnome's keyboard settings and assigned the command

systemctl suspend -i

to it. Which works fine with one hitch: It does not lock the screen when coming out of suspend. If the screen saver is set to lock the screen when it goes on, the screen saver does that for me, but without that the screen just stays unlocked.

I tried to enhance the command assigned to the shortcut to this:

gdbus call --session --dest org.gnome.ScreenSaver --object-path /org/gnome/ScreenSaver --method org.gnome.ScreenSaver.Lock; systemctl suspend -i

This works from the command line, but not as keyboard shortcut. Maybe when invoked as keyboard shortcut the gdbus command does not know which session it was called from?

Anyway; is there a good way to do this - i.e. lock the screen and suspend the computer with a keypress?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2015-05-30 15:41:28 -0500

sensaurav gravatar image

updated 2015-05-30 17:28:32 -0500

You can use xdg-screensaver along with your systemctl command. The following script is an example:-

xdg-screensaver lock; systemctl -i suspend
exit 0

xdg-screensaver has options to control the screensaver manually, and should work in any desktop environment and with any window manager. See man 1 xdg-screensaver for details. If you place this script in a file like $HOME/bin/lock-and-suspend, for example, make it executable by issuing the command chmod +x ~/bin/lock-and-suspend. You can then open the GNOME Control Center Keyboard panel (gnome-control-center keyboard), select Custom Shortcuts, click the Add (+) button, enter any name for your custom command, enter the full path to your script file (/home/username/bin/lock-and-suspend), click within the shortcut assignment area in the right-hand column, and type the shortcut you want. From then on, pressing your selected shortcut key combination should activate the script and lock and suspend the computer.

edit flag offensive delete link more


Hah, it worked! Thank you!

(By the way, the trick was having the two commands in a shell script and calling that script instead of putting

xdg-screensaver lock; systemctl -i suspend

in the command box in the keyboard shortcut definition. When I put my own gdbus and systemctl command sequence in a script it also works.)

It seems that one can not put together commands with a semicolon in that box, it tried it with


which does bring up the calculator from the command line, or a script, but not directly from that command box.

mightyflea gravatar imagemightyflea ( 2015-06-01 15:55:01 -0500 )edit

The semicolon (;) is a statement/command separator in Bash (the text-mode shell), used to separate commands in the shell. Another separator is the double ampersand (&&). The semicolon just runs the commands one after the other, whereas the && runs the second command only if the first one succeeds (returns no error). The command box in the GNOME keyboard panel accepts the path to a program (compiled or script) and just runs it; it will not interpret any shell's language. It will, however, pass any given arguments to the program, e.g., I can enter rfkill block all to turn off wireless devices.

sensaurav gravatar imagesensaurav ( 2015-06-02 09:46:54 -0500 )edit

You could always use sh -c 'xdg-screensaver lock; systemctl -i suspend' to avoid the shell script.

kevincox gravatar imagekevincox ( 2015-11-08 16:12:52 -0500 )edit

Thanks. I had not thought of that. :-)

mightyflea gravatar imagemightyflea ( 2015-11-17 01:47:10 -0500 )edit

answered 2018-06-05 15:48:26 -0500

lovepreetjassal gravatar image

If you use GUI, long press the power button in user menu and the icon will change to suspend after that click it to suspend.

edit flag offensive delete link more

answered 2016-01-13 11:46:02 -0500

Yakko gravatar image


edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-05-29 17:44:47 -0500

Seen: 3,009 times

Last updated: May 30 '15