bash - dbus-monitoring 循环自动退出

标签 bash gnome dbus

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

根据教程和 StackExchange 问题,我想出了以下代码:

#!/bin/bash
while true; do #added to try to solve the issue, but alas it did not
    dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
    while read sign; do
        case "$sign" in
            *"boolean false"*)  echo "Screen unlocked";;
            *"boolean true"*)   echo "Screen locked";;
        esac
    done
done

我使用以下命令启动程序:

nohup myprogram.sh &

开始时一切正常,但一段时间后(几个小时),屏幕锁定/解锁时不再有回显输出。

检查 ps aux | 的输出grep mycommand,我在 开始 时得到以下结果:

user  <pid1> 0.0 0.0 <number> <number> pts/2 S 13:01   0.00 /bin/bash myprogram.sh
user  <pid2> 0.0 0.0 <number> <number> pts/2 S 13:01   0.00 /bin/bash myprogram.sh

在它中断并且不再发出消息后,ps 输出仅显示一行

我正在使用 CentOS 6.5Gnome 2.28(不幸的是我无法升级到任何更新的版本)。


您对可能发生的事情和/或如何进一步调查有任何见解吗?


编辑:更正了 while true;然后语法错误

最佳答案

以下脚本适用于 Linux Mint Cinnamon。注意“肉桂”而不是“侏儒”;如果您不确定要使用什么,请运行 echo $DESKTOP_SESSION,它会为您提供要使用的名称而不是 cinnamon;对我来说:

me@localhost ~] echo $DESKTOP_SESSION
cinnamon

这是脚本:

#!/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

像这样运行:

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

关于bash - dbus-monitoring 循环自动退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55742309/

相关文章:

c - Qtcreator 添加符号时出错 : DSO missing from command line

使用 dbus-send 关闭 Linux

python - 使用python接收dbus信号

mysql - 如何在 bash 脚本中自动设置 mysql 密码?

linux - 如何使用 bash 替换多行?

linux - 如何获取拥有当前事件的 gui session 的用户名?

gnome - 如何重新禁用 gnome-shell 40 中的热角?

linux - 如何使用 Xlib 恢复窗口?

python - 从表中提取特定数据

bash - Node : Bash can't find node?