05 03 | 2013

Suspend your computer from a non-mainstream desktop environment

Written by Tanguy

Classified in : Homepage, Debian, Command line, To remember

Battery icon

UPower for regular users

Major desktop environments usually provide a user-friendly menu to suspend your computer. Internally, if seems to use something called UPower, which uses something called PolicyKit, which in turn may or may not use another piece called ConsoleKit (do not ask me what all these pieces are and how they relate to each other, I do not know and I do not want to know). For the regular user, the result is the following: when they click on that button, it suspends their computer.

UPower for command line users

However, if you are using a more unusual desktop environment, for instance a simple, light window manager, you will not have such a button. And no, UPower does not provide a simple command line tool to suspend your computer¹ (they probably consider that command line is out of fashion…). Well, in case you need it, here is a way to suspend your computer from a command line or whatever: just alias it or bind it to a key shortcut. It uses some dark DBus magic:

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

If you prefer to hibernate your computer, that is, to suspend it, not to RAM but to disk:

$ dbus-send --system --print-reply --dest='org.freedesktop.UPower' \
    /org/freedesktop/UPower org.freedesktop.UPower.Hibernate

As you can see, for some reason it requires some duplication of information: the name “org.freedesktop.UPower” appears three times in that command line: in the “destination”, “object path” and “interface member”, whatever that could mean. Again, do not ask me why, I know nothing about that and I do not want to.

Notes

  1. Actually, UPower does provide a command line tool called upower, but the only thing it does (monitoring power sources) is not really useful for that matter.

12 comments

tuesday 05 march 2013 à 13:02 Pev said : #1

Suspend to disk: s2disk

Suspend to ram: echo -n mem > /sys/power/state

But it requires some rights, I call it with root user.

tuesday 05 march 2013 à 13:50 gregoa said : #2

pm-suspend and pm-hibernate also often work just fine. (package pm-utils; allowing sudo access and binding to a shortcut make them quite comfortable.)

tuesday 05 march 2013 à 14:30 foo said : #3

Go away with upower! ;)
I just use
"echo -n mem > /sys/power/state" or "echo -n disk > /sys/power/state" with some script-foo to suspend to the right location whenever I press the power button.

tuesday 05 march 2013 à 17:11 mbiebl said : #4

upower uses pm-utils as backend which in turn is nothing more then a glorified
echo mem > /sys/power/state resp. echo disk > /sys/power/state

The point of upower is to grant access to those mechanisms without requiring root privileges or explicit setup (via sudo).

tuesday 05 march 2013 à 17:22 Joey Hess said : #5

Every so often I want to go to bed before my laptop is done with some long-running task, but have it suspend later so as not to eat power all night long. Without fail when this happens, I am already tired and invariably cannot remember what the right command line is to use this time. It seems this comes up infrequently enough that the command has often changed since last time...

It seems to me that a distribution like Debian should be able to decide on a command name that makes sense, make it easy to discover with searches like man -k sleep (which comes up empty currently), and arrange for whatever the "right" command is to always be run by that command.

This is an integration problem, it's what distributions are supposed to be good at.

tuesday 05 march 2013 à 17:50 Tanguy said : #6

@Pev, @foo: Writing to /sys/power/state works fine, but it is not sudoable. For that, pm-utils is more appropriate.

@gregoa : Yes, this is an appropriate alternative, with either sudo or setuid permissions and execution limited to a specific group like powerdev.

tuesday 05 march 2013 à 18:27 Paul Menzel said : #7

@Tanguy: What about `echo mem | sudo tee /sys/power/state` to make it “sudoable”?

tuesday 05 march 2013 à 18:53 Manuel Pégourié-Gonnard said : #8

I've been using the commands s2ram, s2disk and s2both from uswsusp (with sudo) for quite some time now, without any problem.

tuesday 05 march 2013 à 21:58 Anonymous said : #9

The three instances of upower on the dbus command line, despite appearances, refer to three different things: an identifier for the upower service on the bus, a path to an object, and a method to execute on that object.

wednesday 06 march 2013 à 10:21 Philipp Kern said : #10

Hm, thanks. It's incredible how long it takes. Also I get a org.freedesktop.DBus.Error.NoReply back, even though the laptop did in fact suspend.

So what I currently use is "i3lock && sudo /usr/sbin/pm-suspend", which is much quicker. But I'm still annoyed that Fn-F4 only works sometimes. With gnome-screensaver in the background it's activated by upower, which is nice. The i3 author refused to implement that dbus call into i3lock, though, claiming that it's not a standard. *sigh*

wednesday 06 march 2013 à 13:12 mirabilos said : #11

JoeyH, in MirBSD, we have the zzz(8) utility – https://www.mirbsd.org/htman/i386/man8/zzz.htm – for that (it’ll only send it to sleep as that’s all APM can do, though). Maybe adding a zzz script (with getopts) that can deal with /sys/power and all that (please no dbus) would be an idea indeed?

wednesday 06 march 2013 à 23:18 Anonymous said : #12

> Maybe adding a zzz script (with getopts) that can deal with /sys/power and all that (please no dbus) would be an idea indeed?

Part of doing the right thing in all environments includes integrating with environments that require UPower or similar DBus interfaces.

Write a comment

What is the first letter of the word lwbk? : 

Archives