stdPikachu
January 27th, 2008, 10:13 PM
Following on my earlier dealings with LIRC (http://ubuntuforums.org/showthread.php?t=678835) 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:
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
SystemCommandsInMenu=HALT;REBOOT;SUSPEND;CUSTOM_CM D
AllowLogoutActions=HALT;REBOOT;SUSPEND;CUSTOM_CMD
banquo@banquo:~$ grep -i hibernate /etc/gdm/gdm.conf | grep -v '#'
HibernateCommand=/usr/sbin/pmi action hibernate
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:~$ whoami
banquo
banquo@banquo:~$ sudo visudo
At the bottom of the sudoers file add a line like this:
banquo ALL= NOPASSWD: /usr/sbin/pmi
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:
%users 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:
banquo@banquo:~$ cat ~/bin/user_hibernate
#!/bin/bash
sudo /usr/sbin/pmi action hibernate
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 :D. 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.
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";
Name: IRExec
Command: /usr/bin/irexec
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:
begin
remote = hauppauge_nova_t
button = power
prog = irexec
config = /home/banquo/bin/user_hibernate
repeat = 0
delay = 0
end
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.
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!
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:
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
SystemCommandsInMenu=HALT;REBOOT;SUSPEND;CUSTOM_CM D
AllowLogoutActions=HALT;REBOOT;SUSPEND;CUSTOM_CMD
banquo@banquo:~$ grep -i hibernate /etc/gdm/gdm.conf | grep -v '#'
HibernateCommand=/usr/sbin/pmi action hibernate
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:~$ whoami
banquo
banquo@banquo:~$ sudo visudo
At the bottom of the sudoers file add a line like this:
banquo ALL= NOPASSWD: /usr/sbin/pmi
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:
%users 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:
banquo@banquo:~$ cat ~/bin/user_hibernate
#!/bin/bash
sudo /usr/sbin/pmi action hibernate
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 :D. 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.
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";
Name: IRExec
Command: /usr/bin/irexec
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:
begin
remote = hauppauge_nova_t
button = power
prog = irexec
config = /home/banquo/bin/user_hibernate
repeat = 0
delay = 0
end
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.
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!