Find the answer to your Linux question:
Results 1 to 4 of 4
Alright, I've got this idea that really only applies to laptops (I believe). AND, in particular to laptops like my old Dell Inspiron 6400 with a battery that will power ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2006
    Location
    near Berea, Kentucky (in a tipi)
    Posts
    47

    "New" Idea: maybe belongs in programming forum...


    Alright, I've got this idea that really only applies to laptops (I believe).

    AND, in particular to laptops like my old Dell Inspiron 6400 with a battery that will power the machine for a few (like 5) minutes, at most.

    (BTW, openSUSE 11.4 Xfce, not that it matters.)

    In /etc/inittab there are the following lines:

    pf:owerwait:/etc/init.d/powerfail start
    pn:owerfailnow:/etc/init.d/powerfail now

    I know these are there to allow UPS software to tell init to do a shutdown if and when the power fails. How does that work? What triggers these events?

    What I'd like is for the pf: line to get kicked if the AC power fails, giving the machine time to shutdown gracefully.

    Does anyone know how to write something that does that? Alternatively, of course, a script could just execute shutdown (provided it had enough privilege.) So, really, the question is, how do I find out if we're running on AC, or get a poke when that ceases to be the case?

    What I'm envisioning is a bash/perl/python script or C program that runs as a daemon, gets the signal from APM or ACPI and either uses inittab or not to shut the machine down.

    Any pointers would be greatly appreciated. Thanks!

  2. #2
    Just Joined!
    Join Date
    Nov 2006
    Location
    near Berea, Kentucky (in a tipi)
    Posts
    47
    Okay, having received no reply... and after a bit more research, here's what I tried:


    $ cat /etc/acpi/events/battery
    event=battery.*
    action=/etc/acpi/actions/battery.sh


    $ cat /etc/acpi/actions/battery.sh
    #!/bin/bash
    # ac/battery event handler
    #
    status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/AC/state`

    if [ "$status" = "off-line" ]
    then
    ## following line still contains -k option (just kidding...)
    /sbin/shutdown -k now "AC Adapter Unplugged... Shutting system down now!!!!"
    else
    #
    /sbin/shutdown -c now "AC Power Restored... System continues"
    fi

    and:
    $ ls -l /etc/acpi/actions/battery.sh
    -rwxr-xr-x 1 root root 626 Aug 16 17:43 /etc/acpi/actions/battery.sh

    /var/log/messages shows acpid getting the correct number of rules.

    I pull the plug and NOTHING HAPPENS.

    I run the battery.sh script explicitly (as root) and it complains it can't find the pid of running shutdown (fair enough since there isn't one.)
    I can also test the awk line on the cl and it works fine.

    so: acpid isn't triggering the script.

    How to debug?

  3. #3
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Have you tried running the acpid daemon in the foreground, with the debug flag on?
    Code:
    service acpid stop
    acpid -d -f
    maybe that will turn up something.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Nov 2006
    Location
    near Berea, Kentucky (in a tipi)
    Posts
    47

    "New" idea: SOLVED

    That was a very useful reply, thank you!

    I found that acpid -f -d -l was needed to get acpid to 'log' to STDERR. That showed me that the 'event' was not a battery event, but an 'ac_adapter' event.

    So:

    Code:
    $ cat /etc/acpi/events/ac_adapter
    event=ac_adapter.*
    action=/etc/acpi/actions/ac_adapter.sh
    and;

    Code:
    $ cat /etc/acpi/actions/ac_adapter.sh
    #!/bin/bash
    # ac/battery event handler
    #                                                                                                                                   
    status=`awk '/^state: / { print $2 }' /proc/acpi/ac_adapter/AC/state`
                                                                                                                                        
    if [ "$status" = "off-line" ]
    then
    	/sbin/shutdown -h now "AC Adapter Unplugged... Shutting system down now!!!!"
    else   
    	/sbin/shutdown -c now "AC Power Restored... System continues"
    fi
    works beautifully!

    Thanks again!
    Daniel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •