Execute a command when power source changes
My laptop has a dead battery. Even when it's fully charged, if the power cable is pulled out it only lasts for about 4 minutes before it dies.
I'm running Ubuntu Lucid Lynx 32-Bit. What I want to do is create some sort of script or program that will immediately set my computer to go into hibernation once it detects that the power cable has gone dead. I'm willing to look into all ways of doing this. I can program in C, but I'm not familiar with the API used to manage Power Management in Linux. I wonder if I could write a simple program something like the following: Code:
int main(void) |
Code:
#echo disk > /sys/power/state |
You can probably use halevt. It's a daemon that monitors hal for system events and lets you configure commands for them.
http://www.nongnu.org/halevt/ I couldn't tell you for sure how to configure it for power supply changes, but you can probably find out by monitoring hal's output with "lshal -m". See their configuration page for more details. |
using hal or udev, you should able to run a command in specific conditions. i'm not sure much about how as it's been a while since i did anything with hal or udev.
|
I found a way of figuring out whether the mains cable is plugged in:
Code:
lshal | grep ac.adapter.present If it's true, I'll do nothing. If it's false, I'll check it again in another 30 seconds' time. If it's still false, then I'll set the machine to hibernate. Here's the C code I've got so far: Code:
#include <stdio.h> /* popen, pclose, fgets */ |
I'm going to repeat my suggestion for halevt again, since you've clearly found the hal event that you need for the trigger. Why go to all the trouble to write your own program when someone has already done it for you?
Besides, I don't believe halevt has to continuously poll your system in order to do it's job; it simply registers and reacts to whatever events hal reports. Plus you have the ability to add more rules to it in the future to do other things if you find the need. I use it to reset my trackball configuration when I hotplug it, for example. |
All times are GMT -5. The time now is 03:04 AM. |