3

In a Java Swing desktop application running on Linux (e.g., Ubuntu), how can the application be notified that the PC has just resumed from the sleep/suspended/hibernated state?

Screen blanking is not a concern. All other suspended or hibernated states (suspend to RAM or suspend to disk) are of interest (and for my purposes they will all be treated the same).

My goal is to invoke a class method automatically when the PC or device is resumed. (That method needs to restart rxtxSerial, fwiw.)

EDIT: A comment from Thorbjørn Ravn Andersen made me realize my question is not clear. My goal is to listen to an operating system event (or utilize a system log file) that would indication the system has just resumed from a suspended state.

MountainX
  • 6,217
  • 8
  • 52
  • 83
  • Have you looked at [Sigar](http://support.hyperic.com/display/SIGAR/Home)? I'm not sure if it can do this, but it sure can do pretty much everything else! I can't boast about this library enough. – Hovercraft Full Of Eels Nov 19 '11 at 21:44
  • The thread linked to in the first comment is about Windows and this one is about Linux. – Hovercraft Full Of Eels Nov 19 '11 at 21:45
  • [Here's](http://superuser.com/questions/357275/how-to-find-the-uptime-since-last-wake-from-standby) another idea I just found on superuser.com. It doesn't provide a solution for me, but it offers food for thought... – MountainX Nov 20 '11 at 00:10
  • OK, so my question was "closed as exact duplicate by Matthew Farwell, Chris, Kublai Khan, Andrew Barber, Clive." Can someone kindly point out where the answer is? I see a Windows-related question. I do not see any exact duplicate nor do I see any solution. – MountainX Nov 20 '11 at 00:36
  • If you guys want to reopen this question I will post my solution. I got a solution via paid Canonical support. – MountainX Nov 22 '11 at 17:33
  • I will vote to re-open, as per my second comment, this question is about a totally different OS than the one that is the "duplicate". I have flagged the moderators for review. – Hovercraft Full Of Eels Nov 22 '11 at 17:46
  • it appears that your question has been re-opened. – Hovercraft Full Of Eels Nov 22 '11 at 19:35

3 Answers3

4

Have a daemon thread read system time, say, once a second. If there is a (noticeable) gap between two readings, your system just woke up. Not quite sure whether this would prevent the system going into sleep in the first place, though.

Mike Adler
  • 1,199
  • 9
  • 24
  • Thanks for the suggestion. In addition to possibly preventing a system from going to sleep (and I too don't know if that would be the case), this solution will also use more power and drain the battery on a laptop. But at least this idea presents one possible solution. I am really hoping to be able to listen to an operating system event. – MountainX Nov 20 '11 at 00:06
0

There is to my knowledge no class in the default Java runtime that provide this information, and there is - also to my knowledge - no standard way of being told these things.

I would suggest that you figure out how to get the information you need from the operating system - e.g. a script that can be installed and then run every time the system changes suspension state - and let that script/native binary send your Java program a message (or put a file in an agreed upon location).

Consider that Linux boxes are multiuser systems and your program may be invoked more than once so you should be careful about what you assume when designing the mechanism.

Out of curiosity, why does your program need to know?

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • `I would suggest that you figure out how to get the information you need from the operating system.` Yes. That is exactly what I am asking for help with. You restated (and probably clarified) my question, but you did not provide an answer. My program needs to know so it can reconnect to a hardware device. – MountainX Nov 20 '11 at 00:14
  • I don't know where the location is under Ubuntu, but I do know that you need to solve this outside the JVM – Thorbjørn Ravn Andersen Nov 20 '11 at 05:05
  • Aldo you didn't say _why_ your program needs to know! There might be a better way to do what you need. – Thorbjørn Ravn Andersen Nov 20 '11 at 05:07
0

Here's what the Canonical support engineer suggested.


20/11/2011 22:53 | Jason My suggestion is to create a script under the /etc/pm/sleep.d/. The script will be execute when resume from suspend. Please reference to the /usr/share/doc/pm-utils/HOWTO.hooks.gz for the details.


Here's the intro from that file:

How to write a pm-utils hook:

PARAMETERS

A pm-utils hook is simply an executable file that accepts at least one parameter.

For hooks in sleep.d, the potential values of the first parameter are:

suspend -- The hook MUST perform whatever action is appropriate when the system is preparing for memory sleep (or its equivalent).

resume -- The hook MUST perform whatever action is appropriate when the system is coming out of suspend.

hibernate -- The hook MUST perform whatever action is appropriate when the system is preparing for suspend-to-disk.

thaw -- The hook MUST perform whatever action is appropriate when the system is coming out of suspend-to-disk.

help -- If your hook parses the PM_CMDLINE environment variable for switches, this function SHOULD output text describing the parameters it parses in a format easily understandable by an end-user.

The actual sleep method being used will be passed as the second parameter -- if your hook needs to handle suspend-hybrid (or any other platform-specific sleep method), it should examine the second parameter.

For hooks in power.d, the potential values of that parameter are: true -- the hook MUST perform whatever action is appropriate when the system transitions TO battery power. false -- The hook MUST perform whatever action is appropriate when the system transitions FROM battery power.

And here's an example:

#!/bin/bash
case "$1" in
hibernate|suspend)
    ACTION BEFORE SUSPEND/HIBERNATE
    ;;
thaw|resume)
    ACTION AFTER RESUME
    ;;
*)
    ;;
esac
exit $?
MountainX
  • 6,217
  • 8
  • 52
  • 83