如何在屏幕睡眠/唤醒上运行脚本

Joh*_*ryk 5 scripts suspend screen wakeup

我想知道如何在屏幕超时和唤醒时运行脚本。我不是在谈论休眠或暂停,而只是在屏幕关闭时。

原因是我有一个 LED 键盘,想在唤醒时切换内置 LED。

小智 6

您可以使用它xset -q来检查显示器的状态。到目前为止,我已经看到监视器的 DPMS 状态为“监视器打开”、“监视器关闭”或“监视器处于挂起状态”。您可以编写一个脚本,然后在 xorg 启动后自动启动:

#!/bin/bash
while true; do
    xset -q | grep "Monitor is On"
    if [ $? -eq 1 ]; then
        if [ "`cat /tmp/displaystate`" != "off" ]; then 
            echo "off" > /tmp/displaystate
            # do something when display is switched off
            /opt/myScreenOffAction.sh
        fi
        sleep 1
    else 
        if [ "`cat /tmp/displaystate`" != "on" ]; then 
            echo "on" > /tmp/displaystate
            #do something when display is switched on
            /opt/myScreenOffAction.sh
        fi
        sleep 10
    fi
done
Run Code Online (Sandbox Code Playgroud)

要测试您的脚本,您可以使用它xset dpms force suspend来暂停屏幕。

我建议你 grep 为“Monitor is On”,因为上面提到的 off 似乎有不同的状态。