4

I have a backup script that is run each night. If my (Linux-) host is suspended it is woken up by another host before the script runs.

I would like to be able to suspend my host again after the backup is finished, but only if it was woken up to run the script. If it was running before, I want to leave it running.

Is there any way I can get the information when the host was last woken from suspend-to-ram?

2
  • are you using Tuxonice or normal kernel hibernation ?
    – daisy
    Oct 6, 2011 at 13:32
  • Not using hibernation at all, just suspend to RAM. Oct 6, 2011 at 20:30

2 Answers 2

3

Your distribution has a power management framework (this is clear because it is already suspending itself) that runs some scripts when it is about to suspend/hibernate/shutdown as well as when it resumes/turns on. If you do a search for " power management", you should be able to find good documentation about it.

The task probably involves simply placing a script in an appropriate directory. That script can be as simple as date +%s > /var/last-wake-time. Then your existing script would simply look at that file.

In general, not every distribution comes with power management utilities, and there are several tools tools to look at. You can check out acpid, uPower, pm-utils, and laptop-mode tools.

1
  • Yes, this should be possible and I'll probably go that route. It would however be nice to have something that works without configuration. Oct 6, 2011 at 13:26
3

I ended up checking the pm-utils log file because this works out-of-the-box. However it depends on the formating of the pm-utils log messages, so it could break with new versions of pm-utils.

It's currently working on Arch Linux and OpenSuSE 11.4/Tumbleweed and probably all other distributions that haven't patched pm-utils too much.

The following code sets NEED_SUSPEND to 1 if the machine was woken from suspend within the last 10 minutes (600 seconds):

# Check if we need to suspend afterwards
if [[ -e /var/log/pm-suspend.log ]]; then
    RESUME_DATE="$(egrep 'Running hooks for (resume|thaw)' /var/log/pm-suspend.log | tail -n 1 | sed 's/^\(.*\):.*$/\1/')"
    if [[ -n "$RESUME_DATE" ]]; then
        RESUME_SECS="$(date --date="$RESUME_DATE" +%s)"
        NOW_SECS="$(date +%s)"
        SECS_SINCE_RESUME=$(( $NOW_SECS - $RESUME_SECS ))
        if [[ $SECS_SINCE_RESUME -lt 600 ]]; then
            NEED_SUSPEND=1
        fi
    fi
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .