1

So I have a little systemd-script which is supposed to hibernate my laptop after it has been in suspend mode after x amount of seconds (filename is /lib/systemd/system-sleep/0000rtchibernate.sh):


#!/bin/bash
# Script name: /usr/lib/systemd/system-sleep/0000rtchibernate.sh
# Purpose: Auto hibernates after a period of sleep
# Edit the "autohibernate" variable below to set the number of seconds to sleep.
curtime=$(date +%s)
autohibernate=10  #for testing purposes
sleeptime=2
logging=true
logfile="/lib/systemd/system-sleep/0000rtchibernate.log"
skipfile="/lib/systemd/system-sleep/0000rtchibernate.skip"
lockfile="/tmp/rtchibernate.lock"
echo "$curtime $1" >>/tmp/autohibernate.log

if [ "$2" = "hibernate" ]; then
        if [ "$logging" = true ]; then
            echo "$(date) : Hibernating, doing nothing" >> $logfile
        fi

    exit 0
fi

if [ "$1" = "pre" ]; then
    # only set timer if file 0000rtchibernate.skip doesn't exist
    if [ ! -f $skipfile ]; then
        # Suspending.  Record current time, and set a wake up timer.
        if [ "$logging" = true ]; then
            echo "$(date) : Suspending" >> $logfile
        fi
        echo "$curtime" > $lockfile
        rtcwake -m no -s $autohibernate
    fi
fi

if [ "$1" = "post" ]; then
    if [ -f $skipfile ]; then
        # if skipfile is present on resume, remove it
        if [ "$logging" = true ]; then
            echo "$(date) : Removing skipfile" >> $logfile
        fi
        rm $skipfile
    else
        # Coming out of sleep
        sustime=$(cat $lockfile)
        rm $lockfile

        # Did we wake up due to the rtc timer above?
        if [ $(($curtime - $sustime)) -ge $((autohibernate-5)) ]; then

            if [ "$logging" = true ]; then
                echo "$(date) : Hibernating" >> $logfile
            fi

            # Then hibernate
            /bin/systemctl hibernate
            return_val=$?
            if [ "$logging" = true ]; then
                echo "$(date) : Return value of systemctl hibernate was $return_val" >> $logfile
            fi
            # if hibernation fails, create skipfile and suspend
            if [ "$return_val" -gt 0 ]; then
                if [ "$logging" = true ]; then
                    echo "$(date) : Hibernation failed. Creating skipfile and suspending" >> $logfile
                fi
                touch $skipfile
                /bin/systemctl suspend
            fi

        else
            if [ "$logging" = true ]; then
                echo "$(date) : Waking up early, not hibernating" >> $logfile
            fi
            # Otherwise cancel the rtc timer and wake up normally.
            rtcwake -m no -s 1
        fi
    fi
fi

(code is originally from here, I only adapted it to systemd)

The scripts runs fine when suspending the laptop, here is the logging output:


Di 20. Mär 10:53:31 CET 2018 : Suspending
Di 20. Mär 10:53:44 CET 2018 : Hibernating
Di 20. Mär 10:53:44 CET 2018 : Return value of systemctl hibernate was 1
Di 20. Mär 10:53:44 CET 2018 : Hibernation failed. Creating skipfile and suspending

My problem is that both /bin/systemctl hibernate and /bin/systemctl suspend have a return value of 1 and don't do what they're supposed to, although when running from a terminal window everything is fine. Btw I'm on Ubuntu 18.04. Any help appreciated :-)

  • Hey, I've come across the exact same issue as you (I'm on Ubuntu 18.04 as well), in the end I just ended up going with the solution here which worked great. – eugenhu May 28 '18 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.