关于bash:dbus-monitoring循环自动退出

dbus-monitoring loop exits automatically

我正在尝试在屏幕锁定/解锁时执行bash命令。

以下教程


1
2
3
4
5
6
7
8
9
10
#!/bin/bash
while true; do
    dbus-monitor --session"type='signal',interface='org.gnome.ScreenSaver'" |
    while read -r sign; do
        case"$sign" in
            *"boolean false"*)  echo"Screen unlocked";;
            *"boolean true"*)   echo"Screen locked";;
        esac
    done
done

我将while true; then更改为while true; do并将选项-r添加到while read sign; do


以下脚本在Linux Mint Cinnamon上对我有用。注意"肉桂"而不是"侏儒";如果不确定使用什么,请运行echo $DESKTOP_SESSION,这将为您提供使用的名称,而不是肉桂。对我来说:

1
2
me@localhost ~] echo $DESKTOP_SESSION
cinnamon

这是脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

while true; do #added to try to solve the issue, but alas it did not
    dbus-monitor --session"type='signal',interface='org.cinnamon.ScreenSaver'" |
    while read sign; do
        case"$sign" in
            *[Ff]alse*) echo"Screen unlocked";;
            *[Tt]rue*)  echo"Screen locked";;
            *)   echo"`date` Unknown";;
        esac
        sleep 0.250
    done
done

像这样运行:

1
nohup ./myprogram </dev/null >| $HOME/myprogram.out 2>&1 &