そこで、ラップトップがサスペンド モードになってから 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 を持ち、期待どおりに動作しないことです。ちなみに、私は Ubuntu 18.04 を使用しています。どなたか助けていただければ幸いです :-)