【问题标题】:dbus-monitoring loop exits automaticallydbus-monitoring 循环自动退出
【发布时间】:2019-09-08 13:43:04
【问题描述】:

我正在尝试在屏幕锁定/解锁时执行 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; then 语法错误

【问题讨论】:

  • 没有运行你的代码——但我个人会在循环中引入一个小睡眠。
  • 顺便说一句,外循环也应该是while true; do
  • 在case语句中添加一行*) echo "`date` Unknown";;,以nohup myprogram.sh &lt;/dev/null &gt; $HOME/myprogram.out 2&gt;&amp;1 &amp;运行命令。

标签: bash gnome dbus


【解决方案1】:

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

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 &

【讨论】:

    【解决方案2】:
    #!/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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 2011-01-22
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      相关资源
      最近更新 更多