直接转到下面的第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去打断我呢?
因此,我更新我的脚本如下:
#!/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我发现“关闭”标记,但我没有找到“打开”标记。“开放”标记出现的时间较晚,似乎是随机的。
发布于 2022-02-27 13:47:35
你的脚本有很多问题。
我看到的第一个问题是,您没有像指南中那样检查/proc/acpi/按钮/ lid /*/状态中的盖子状态。您的脚本将在打开和关闭操作时被触发,您需要自己添加检查。
其次,在打开盖子时,需要一些机制来停止已经运行的脚本实例。
https://unix.stackexchange.com/questions/692230
复制相似问题