[HowTo] Suspend Ubuntu from terminal or keyboard shortcuts

When learning to make bash scripts, at one point or other you’d like to know how to suspend your computer from terminal. Well, there are quite some methods available for Ubuntu. You can choose whatever suits your needs. I prefer method 3 (look below).

Method 1:

sudo sh /etc/acpi/sleep.sh force

Method 2:

sudo pm-suspend

This command might not be available in older versions of Ubuntu. It works perfectly fine on Lucid and Maverick.

Method 3:

This command does not require sudo so it might come in handy. For example, you can map it to a keyboard shortcut for easy access.

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

One thing to notice here is none of the commands I mentioned here will ask your password on resuming. For that you’ll have to lock your computer before suspending. You’ll have to use gnome-screensaver-command --lock before suspending to lock the computer

Create a suspend script:

Open a new file in gedit:

sudo gedit /usr/bin/suspend-comp

Copy and paste the following code and save it:

#!/bin/sh 
sleep $1;
dbus-send --print-reply --system --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend

If you want the script to ask for a password on resuming, copy-paste this code instead:

#!/bin/sh 
sleep $1;
gnome-screensaver-command --lock
dbus-send --print-reply --system --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend

Add the executable bit:

sudo chmod +x /usr/bin/suspend-comp

Now suspend-comp time-in-seconds can be used in terminal. For example, if I want to have my computer suspended after 20 seconds I can simply type:

suspend-comp 20

Mapping it to keyboard shortcut for easy access:

Go to System->Preferences->Keyboard Shortcuts. Click Add. Type Suspend for Name and suspend-comp 0 in Command field. Click Apply.

Now scroll to the bottom of the list to find Suspend. Click on ‘Disabled’ on the right to ‘Suspend’. Now press any key combination to map the command to it. I used Ctrl+Alt+Shift+s.

Now simply pressing Ctrl+Alt+Shift+s will suspend your computer.

Cheers!

Leave a comment

Your email address will not be published.