Keyboard Shortcut for Suspend/Sleep Laptop (Ubuntu)

To create a keyboard shortcut for putting your laptop to sleep/suspend mode with lockscreen on return: Copy the below script into a file

#!/bin/bash
gnome-screensaver-command --lock && dbus-send --system --print-reply \
--dest="org.freedesktop.UPower" \
/org/freedesktop/UPower \
org.freedesktop.UPower.Suspend

exit 0

Before naming the file, check if a command of that name doesn’t already exist. you can do this by the which command

Name the file to the command you wanna use. I use compsleep.

With the terminal, reach the file, and convert it into an executable:

chmod +x compsleep

now copy this file into the bin folder, as done at the end of this post.
Check the command in the terminal by writing the filename and pressing enter. If this works, proceed to next step.

Go to
system settings >> (Keyboard Shortcuts)/(Keyboard>>shortcuts)>>+ or add new shortcut.

Enter name of file we created above in the command text box. Set a shortcut for it.

Cheers.

[Update:] custom shortcuts with Alt don’t work from 12.04 onward. It’s a bug.

[Update:2 (August 2016)] The above mentioned command doesn’t work on the newer systems. A simple replacement to that is this:

#!/bin/bash
systemctl suspend
exit 0

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s