版本1
直接进入下面的版本2
在 Debian 上,我遵循本指南定义笔记本电脑盖子关闭时的行为。
所以我做了以下修改:
#!/bin/sh
# Disable sleep on lead (do noting)
echo 'HandleLidSwitch=ignore' | tee --append /etc/systemd/logind.conf
echo 'HandleLidSwitchDocked=ignore' | tee --append /etc/systemd/logind.conf
sudo service systemd-logind restart
# Disable screen on lid close/etc/acpi/events
mkdir -r /etc/acpi/events
echo 'event=button/lid.*' | tee --append /etc/acpi/events/lm_lid
echo 'action=/etc/acpi/lid.sh' | tee --append /etc/acpi/events/lm_lid
# The lid.sh should be in the same directory
mv ./lid.sh /etc/acpi/lid.sh
chmod +x /etc/acpi/lid.sh
# Restarting service to take effect
/etc/init.d/acpid restart
然后我创建/etc/acpi/lid.sh
每次关闭盖子时都会激活的行为,而不是默认行为。
就是lid.sh
下面这个:
#!/bin/bash
#
# Lid closing event script
grep -q close /proc/acpi/button/lid/*/state
if [ $? = 0 ]; then
echo close>>/tmp/screen.lid
# main user
USER=fauve
# Remaining percent of battry
REMAININGBAT=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep percentage | sed "s/ *percentage: *\(.*\)%/\1/")
# Test battry remaining percentage
if [ $REMAININGBAT -gt 10 ]; then
# If the percentage is higher than 10
TIMEBEFORELOCK="1m"
TIMEBEFORESUSPENSION="9m"
TIMEBEFORESHUTDOWN="5 minutes"
else
# If the percentage is less than 10
TIMEBEFORELOCK="1m"
TIMEBEFORESUSPENSION="2m"
TIMEBEFORESHUTDOWN="4 minutes"
fi
# Do noting a ${TIMEBEFORELOCK} time
# In case if a quick shifting from a room to another
echo "$(date): Waiting ${TIMEBEFORELOCK} before lock"
sleep ${TIMEBEFORELOCK}
# set screensaver for ${TIMEBEFORESUSPENSION} time
echo "$(date): Lock now"
su -c "DISPLAY=:0.0 /home/$USER/.local/bin/screenlock" - $USER &
echo "$(date): Waiting ${TIMEBEFORESUSPENSION} before suspension"
sleep ${TIMEBEFORESUSPENSION}
# Set a wake up at given time
echo "$(date): Seting time before waking up to ${TIMEBEFORESHUTDOWN}"
echo `date '+%s' -d "+ ${TIMEBEFORESHUTDOWN}"` > /sys/class/rtc/rtc0/wakealarm
# Suspend for ${TIMEBEFORESHUTDOWN} time
echo "$(date): Starting suspention for ${TIMEBEFORESHUTDOWN}"
systemctl suspend
echo "$(date): Wake up"
# Wait 5s to ensure the next command will take effect
sleep 5s
# shutdown
#echo "$(date): Shutdown"
#shutdown +0
fi
grep -q open /proc/acpi/button/lid/*/state
if [ $? = 0 ]; then
echo open>>/tmp/screen.lid
fi
只要我不打开盖子,这个脚本就会继续运行。但当盖子打开时,它应该被中断(如果尚未到达最后一行)。
那么,如何让 ACPI 在打开盖子时中断呢?
版本2
所以,我更新我的脚本如下:
#!/bin/bash
#
# Lid closing event script
PIDFILE=/tmp/lidpid
grep -q close /proc/acpi/button/lid/*/state
if [ $? = 0 ]; then
echo $$ > $PIDFILE
echo "$(date) close">>/tmp/screen.lid
# main user
USER=fauve
# Remaining percent of battry
REMAININGBAT=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep percentage | sed "s/ *percentage: *\(.*\)%/\1/")
# Test battry remaining percentage
if [ $REMAININGBAT -gt 10 ]; then
# If the percentage is higher than 10
TIMEBEFORELOCK="1m"
TIMEBEFORESUSPENSION="9m"
TIMEBEFORESHUTDOWN="5 minutes"
else
# If the percentage is less than 10
TIMEBEFORELOCK="1m"
TIMEBEFORESUSPENSION="2m"
TIMEBEFORESHUTDOWN="4 minutes"
fi
# Do noting a ${TIMEBEFORELOCK} time
# In case if a quick shifting from a room to another
echo "$(date): Waiting ${TIMEBEFORELOCK} before lock"
sleep ${TIMEBEFORELOCK}
# set screensaver for ${TIMEBEFORESUSPENSION} time
echo "$(date): Lock now"
su -c "DISPLAY=:0.0 /home/$USER/.local/bin/screenlock" - $USER &
echo "$(date): Waiting ${TIMEBEFORESUSPENSION} before suspension"
sleep ${TIMEBEFORESUSPENSION}
# Set a wake up at given time
echo "$(date): Seting time before waking up to ${TIMEBEFORESHUTDOWN}"
echo `date '+%s' -d "+ ${TIMEBEFORESHUTDOWN}"` > /sys/class/rtc/rtc0/wakealarm
# Suspend for ${TIMEBEFORESHUTDOWN} time
echo "$(date): Starting suspention for ${TIMEBEFORESHUTDOWN}"
systemctl suspend
echo "$(date): Wake up"
# Wait 5s to ensure the next command will take effect
sleep 5s
echo "Going to shutdown"
# shutdown
#echo "$(date): Shutdown"
shutdown +0
fi
# Wait 5s to ensure the next command will take effect
sleep 5s
grep -q open /proc/acpi/button/lid/*/state
if [ $? = 0 ]; then
echo "$(date) open">>/tmp/screen.lid
if [ -e $PIDFILE ] ; then
kill -9 $(cat $PIDFILE)
fi
fi
但现在的问题是 ACPI 在检测盖子打开时遇到一些困难。当我关闭并打开盖子时,我找到/tmp/screen.lid
“关闭”标记,但没有找到“打开”标记。 “开放”标记出现得较晚,而且似乎是随机的。
答案1
好吧,您的脚本存在多个问题。
我看到的第一个问题是,您没有像指南中那样检查 /proc/acpi/button/lid/*/state 中盖子的状态。您的脚本将在打开和关闭操作时触发,您需要自己添加检查。
其次,您需要某种机制来在打开盖子时停止已经运行的脚本实例。