LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-20-2010, 03:24 AM   #1
Virchanza
LQ Newbie
 
Registered: Aug 2009
Posts: 26

Rep: Reputation: Disabled
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)
{
    for (;;)
    {
        if ( !IsMainsPowerConnected() )
        {
            CommenceImmediateHibernation();
            sleep(200);
        }

        sleep(30);
    }
}
 
Old 05-20-2010, 06:59 AM   #2
ozanbaba
Member
 
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778

Rep: Reputation: 135Reputation: 135
Code:
#echo disk > /sys/power/state
this will cause the computer to hybernate.
 
Old 05-20-2010, 07:51 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 05-20-2010, 11:38 AM   #4
ozanbaba
Member
 
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778

Rep: Reputation: 135Reputation: 135
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.
 
Old 05-21-2010, 08:20 AM   #5
Virchanza
LQ Newbie
 
Registered: Aug 2009
Posts: 26

Original Poster
Rep: Reputation: Disabled
I found a way of figuring out whether the mains cable is plugged in:

Code:
lshal | grep ac.adapter.present
I'm gonna check this value every 30 seconds.

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 */
#include <string.h> /* strchr */
#include <unistd.h> /* sleep */

int Is_AC_Unplugged(void)
{
        char line[200];

        /* Run the command, take one line from it, then close the opened stream */
        FILE *pfile = popen("lshal | grep ac.adapter.present", "r");
        fgets(line, sizeof line, pfile);
        pclose(pfile);

        /* Find the position of the '=' sign in the line of text */
        char const *p = strchr(line,'=');

        if (!p)
        {
            /* Dodgy reading, there's no '=' sign */
            return 0;
        }

        /* Now check for the first 't' or 'f' after the '=' sign */
        for (;;)
        {
            ++p;

            if (!*p)
            {
                /* We've already reached the null terminator, must be a dodgy reading! */
                return 0;
            }

            if (  (*p == 't') || (*p == 'T')  )
            {
                /* The power adapter is plugged in, no need to do anything */
                return 0;
            }
            else if (  (*p == 'f') || (*p == 'F')  )
            {
                /* Oh shit the power adapter is unplugged */
                return 1;
            }
        }
}

void Deal_With_Unplugged_AC_Adapter(void)
{
    puts("We've lost AC power. Now we wait 30 seconds to see if it's still gone.");

    sleep(30);

    if (!Is_AC_Unplugged())
    {
        /* The power must be back, abort the Hibernation */
        puts("Wahey the power's back, no need to hibernate!");
        return;
    }

    /* OK so the power is still gone, let's Hibernate */

    puts("HIBERNATING. . . ");

    /* Insert code here to make computer hibernate */
}

int main(void)
{
    for ( ; ; sleep(30))
    {
        if (Is_AC_Unplugged())
        {
            Deal_With_Unplugged_AC_Adapter();
        }
        else
            puts("We still have AC. . .");
    }
}
 
Old 05-21-2010, 09:43 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to execute Source command in a shell script UltraSoul Linux - General 3 09-23-2010 10:13 AM
Only execute 2nd command on successufl execution of 1st command kregec05 Linux - Newbie 3 08-19-2009 10:29 AM
Execute a command , reboot and then execute another command ganeshp@moris.org Linux - Newbie 3 12-03-2008 12:51 AM
remote command execute telnet with 1 command MPowers Linux - Networking 2 06-30-2005 06:31 AM
command execute without ./ kaon Slackware 11 09-15-2004 05:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration