Following on my earlier dealings with LIRC I've been tackling how to use LIRC to shut down the machine from the comfort of your sofa. You'll still need to get off your lazy **** to turn it back on unless you have some nifty wake-on-something-else set up.
Note that the rest of this howto assumes that you've already tested suspend/hibernate and that you know your machine can wake up properly from it. In my case, suspend isn't an option (argh) because my TV doesn't detect that the machine has been turned back on again; I use hibernate to force the BIOS to go through VGA POST again, and it's still faster than booting. However, the instructions here should be a good starting point to cannibalise for your own purposes.
My first problem was finding out which commands were used in ubuntu to shutdown, hibernate, etc, until someone pointed me in the direction that it was actually the login manager (which runs as root) that took care of all that, not the DE itself (which runs as you). With that in mind I had a look in the GDM config:
So we know we need to set the program `pmi` to run as root, since a regular user doesn't have the privelige. This is accomplished with sudo. First we need to know who we're running as, and then edit the sudoers file.
banquo@banquo:~$ grep -i shutdown /etc/gdm/gdm.conf | grep -v '#'
RebootCommand=/sbin/shutdown -r now "Rebooted via gdm."
HaltCommand=/sbin/shutdown -h now "Shut Down via gdm."
banquo@banquo:~$ grep -i suspend /etc/gdm/gdm.conf | grep -v '#'
SuspendCommand=/usr/sbin/pmi action suspend
banquo@banquo:~$ grep -i hibernate /etc/gdm/gdm.conf | grep -v '#'
HibernateCommand=/usr/sbin/pmi action hibernate
At the bottom of the sudoers file add a line like this:
banquo@banquo:~$ sudo visudo
Save and quit. That entry basically says that the user "banquo" can act as root, without having to type in a password first, but ONLY for the purposes of running `/usr/sbin/pmi`. Technically, this is a slight security risk (running anything as root without proper oversight is a risk - although I beleive the benefits outweigh the downsides in this case) and be very wary of adding lines to your sudoers file in this fashion. Those of you with multiuser setups might even want to go to the lengths to add all users to this sudo rule:
banquo ALL= NOPASSWD: /usr/sbin/pmi
Now that's done you should be able to run `sudo pmi action hibernate` from your standard shell without having to enter your password, and the machine should hibernate. We can put this is a script if we like:
%users ALL= NOPASSWD: /usr/sbin/pmi
Don't forget to make it executable! You should be able to `./bin/user_hibernate` and have the machine hibernate as if you'd run the command itself. Similarly, you can use the contents of this mini-howto to execute any script you wish via IRExec, even to the point of restarting your apache server or starting global thermonuclear war - such is the power of UNIX . Even more mind-nimbingly simple, you could just set a keyboard shortcut to run the script as well (finally, a use for all those stupid multimedia keys!). I've not tested it, but you should also be able to define a sudoers rule for this script itself rather than for pmi and run that with root privs instead, probably a more secure setup.
banquo@banquo:~$ cat ~/bin/user_hibernate
sudo /usr/sbin/pmi action hibernate
One gotcha to be aware of: remember when you run things as another user, they're running as another user Sounds very daft, but just remember than if you `sudo ~/bin/someapp`, the ~ will evaluate to /root instead of /home/youruser and you'll end up running the wrong program (or, more frequently, a program that doesn't exist). Similarly, any custom aliases or suchlike that live in your user profile won't exist for root. This is another reason why it's often a good idea to use absolute paths wherever possible.
Once that's working we can move on to IRExec.
IRExec is an LIRC program used to launch arbitary commands for apps that don't support LIRC natively (like Myth does). Slightly annoyingly, it isn't started by default so we have to create our own launcher for it. Assuming you're using XFCE you can go to Applications > Settings > Autostarted applications (you should see network manager and mythfrontend in there already) and click "add";
Once we've got IRExec running all we need to do is assign one of the keys on our remote and get it to point at the script we made; this is also very easy to do. Go into your ~/.lircrc file and create an entry like this:
Please note that you shouldn't use a button that's already defined for another program; IRExec will ALWAYS execute it, even if you're running MythTV at the time. As it is, my "power" button isn't assigned any other duties. If you want to test whether irexec is working, set the config to "beep" and a press on the button should make a beep come out of your computer speaker.
remote = hauppauge_nova_t
button = power
prog = irexec
config = /home/banquo/bin/user_hibernate
repeat = 0
delay = 0
Note that it's possible you may press the remote accidentally; I'm working on methods to mitigate this at the moment (a popup in mythnotify perhaps? With, say, feedback and a 30s grace time?). I'd also like to use mythshutdown, but it doesn't support any of the funkier things like hibernation or suspend so I'm ignoring it for now. If anyone enterprising out there has something similar I'd love to hear about it.
Happy power-and-noise savings!