首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当笔记本电脑盖子打开时,如何打开/etc/acpi/lid.sh?

当笔记本电脑盖子打开时,如何打开/etc/acpi/lid.sh?
EN

Unix & Linux用户
提问于 2022-02-26 18:22:08
回答 1查看 653关注 0票数 1

版本1

直接转到下面的第2版

在Debian上,我跟随本指南定义了一种当我的笔记本电脑的盖子关闭时的行为。

因此,我做了以下修改:

代码语言:javascript
复制
#!/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如下所示:

代码语言:javascript
复制
#!/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版

因此,我更新我的脚本如下:

代码语言:javascript
复制
#!/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我发现“关闭”标记,但我没有找到“打开”标记。“开放”标记出现的时间较晚,似乎是随机的。

EN

回答 1

Unix & Linux用户

发布于 2022-02-27 13:47:35

你的脚本有很多问题。

我看到的第一个问题是,您没有像指南中那样检查/proc/acpi/按钮/ lid /*/状态中的盖子状态。您的脚本将在打开和关闭操作时被触发,您需要自己添加检查。

其次,在打开盖子时,需要一些机制来停止已经运行的脚本实例。

票数 2
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/692230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档