systemd 스크립트에서 실행될 때 "systemctl hibernate"가 1을 반환함

systemd 스크립트에서 실행될 때 "systemctl hibernate"가 1을 반환함

그래서 x초 후에 일시 중지 모드에 들어간 후 랩톱을 최대 절전 모드로 전환하도록 하는 작은 systemd 스크립트가 있습니다(파일 이름은 /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

(코드는 원래여기, 나는 그것을 systemd에만 적용했습니다)

랩톱을 일시 중지하면 스크립트가 제대로 실행됩니다. 로깅 출력은 다음과 같습니다.


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

내 문제는 둘 다 /bin/systemctl hibernate반환 /bin/systemctl suspend값이 1이고 예상한 대로 수행되지 않는다는 것입니다. 하지만 터미널 창에서 실행할 때는 모든 것이 정상입니다. Btw 나는 우분투 18.04를 사용하고 있습니다. 도움을 주시면 감사하겠습니다 :-)

관련 정보