Sewi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I need to detect Standby and Hibernation mode from a script.

My script is using a serial device which needs to be re-init'ed when the computer (Ubuntu 10) is returning from Standby or Hibernation mode. Is there any chance for a Perl script to detect this condition?

Thanks!

Replies are listed 'Best First'.
Re: Detect standby/hibernation mode
by moritz (Cardinal) on Jun 14, 2011 at 19:41 UTC
    The existing hibernate/standby system has hooks for programs to be executed after returning from sleep, usually to be found in /etc/acpi/events/.

    Check the documentation of hibernate and acpi, it's much easier than trying to monitor such things from within perl.

Re: Detect standby/hibernation mode
by BrowserUk (Patriarch) on Jun 14, 2011 at 19:54 UTC

    One simple mechanism would be to run a background thread that check the time once per second and sets a flag if the difference between this time and last time is greater than (say) 2 seconds. This 'worked' for me:

    #! perl -slw use strict; use threads; use threads::shared; my $sem :shared = 0; async { my $last = time; while( sleep 1 ) { my $now = time; if( $now - $last > 2 ) { $sem = 1; } $last = $now; } }->detach; while( sleep 1 ) { print "tum te tum"; if( $sem ) { print "You snoozed me"; $sem = 0; } } __END__ C:\test>junk96 tum te tum tum te tum tum te tum tum te tum tum te tum tum te tum tum te tum tum te tum tum te tum You snoozed me tum te tum tum te tum tum te tum tum te tum Terminating on signal SIGINT(2)

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.