Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
are you using Tuxonice or normal kernel hibernation ? –  warl0ck Oct 6 '11 at 13:32
    
Not using hibernation at all, just suspend to RAM. –  Mika Fischer Oct 6 '11 at 20:30
add comment

2 Answers

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.

share|improve this answer
    
Yes, this should be possible and I'll probably go that route. It would however be nice to have something that works without configuration. –  Mika Fischer Oct 6 '11 at 13:26
add comment
up vote 2 down vote accepted

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.