Power Management from the Command Line

To be able to invoke commands like suspend and hibernate from the command line not so long ago required having root privileges or using the desktop environment built-in tools. Now to invoke suspend, hibernate, shutdown, or restart, D-Bus can be invoked as Regular user. I created a script called pwrman to ease the task (requires UPower to be installed).

(I got this idea from a person from the Arch Linux forums. I forgot who you are, so sorry, but thank you.)

#!/bin/bash
# Power management as a regular user

scrpt=${0##*/}  # filename of script

case $1 in
  shutdown  | S )
    echo " Shutting Down..."
    dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit" \
    /org/freedesktop/ConsoleKit/Manager \
    org.freedesktop.ConsoleKit.Manager.Stop > /dev/null                       ;;
  restart   | R )
    echo " Restarting System..."
    dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit" \
    /org/freedesktop/ConsoleKit/Manager \
    org.freedesktop.ConsoleKit.Manager.Restart > /dev/null                    ;;
  suspend   | s )
    echo " Suspending to RAM..."
    dbus-send --system --print-reply --dest="org.freedesktop.UPower" \
    /org/freedesktop/UPower org.freedesktop.UPower.Suspend > /dev/null        ;;
  hibernate | h )
    echo " Suspending to Disk..."
    dbus-send --system --print-reply --dest="org.freedesktop.UPower" \
    /org/freedesktop/UPower org.freedesktop.UPower.Hibernate > /dev/null      ;;
  monitor | m  )
    echo " Sleeping Monitor..."
    sleep 1 && xset s activate                                                ;;
  *         )
    echo " $scrpt <shutdown(S)|restart(R)|suspend(s)|hibernate(h)|monitor(m)>"
esac

About Todd Partridge (Gen2ly)

Good times, good people, good fun.

12 thoughts on “Power Management from the Command Line

  1. Derek Schrock says:

    #!/usr/bin/env bash

  2. Kazuo says:

    Something is not right, copy and pasted, and I got:
    % bash pwrman shutdown
    pwrman: line 4: filename: command not found
    pwrman: line 6: $’case\302\240shutdown\302\240in’: command not found
    pwrman: line 7: syntax error near unexpected token `)’
    pwrman: line 7: ` shutdown )’

    Can you give a wgeteable link? But the idea is very useful.

  3. Kai Hendry says:

    DBus? Urgh, how many millions of lines code were used to do something relatively simple?

    This isn’t in keeping with the Arch philosophy. Come on, do better!

  4. @ Kazuo

    Can you give a wgeteable link? But the idea is very useful.

    Sure:

    wget http://pastebin.com/download.php?i=9EMhQSg6 -O pwrman

    Something is not right, copy and pasted, and I got:

    % bash pwrman shutdown
    pwrman: line 4: filename: command not found
    pwrman: line 6: $'case\302\240shutdown\302\240in': command not found
    pwrman: line 7: syntax error near unexpected token `)'
    pwrman: line 7: `  shutdown  )'

    Not sure about this. Could it have something to do with your shell? This is pretty basic bash syntax.

    @ Kai Hendry

    DBus? Urgh, how many millions of lines code were used to do something relatively simple?

    This isn’t in keeping with the Arch philosophy. Come on, do better!

    Actually, I had this script gotten before from the Arch forums, but the commands needed updating and I found them on the Arch Wiki. Invoking dbus, this is as good as it gets.

    @ Derek Schrock

    #!/usr/bin/env bash

    Eh?? I’ve seen this before but sometimes in older scripts. Doesn’t seem to be the common way to do things anymore. Any reason I should be using this syntax?

  5. Kai Hendry says:

    What about `sudo halt` & `sudo reboot` or just making sure the physical buttons work on the PC?

  6. @ Kai Hendry

    What about `sudo halt` & `sudo reboot` or just making sure the physical buttons work on the PC?

    Yeah, I hear you. I basically thought this solution was useful because of having to call sudo and entering a password to something I had physical access to just seeming unnecessary to me. My basic use for it is usually to suspend quickly when doing work from the command line which I often do. Perhaps using dbus doesn’t make the prettiest looking script, but it does work nice and I expect it to be around awhile.

  7. Kai Hendry says:

    Use the NOPASSWD sudo keyword in your `/etc/sudoers`.

    DBus is to be avoided. It sucks.

  8. Sure, that does a good job. I find that I reinstall from time to time so having this script around work a bit better for me.

  9. Kazuo says:

    @Todd
    Thanks. Solved. The problem come from the copy/paste it copied some non-space spaces.

  10. Schumbi says:

    Hey, nice script thanks. :-) I thinking about using it in combination with i3.
    If you use the script don’t forget to use a dbus-session. Will likely not work without one.

  11. UbuntuNotUnity says:

    Works in Ubuntu beta, thanks.

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 )

Connecting to %s