Bash dbus监视循环自动退出
我试图在屏幕锁定/解锁上执行bash命令。 在以下教程和StackExchange问题中,我提出了以下代码:Bash dbus监视循环自动退出,bash,gnome,dbus,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 cas
#!/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
user 0.0 0.0 pts/2 S 13:01 0.00/bin/bash myprogram.sh
用户0.0 0.0 pts/2 S 13:01 0.00/bin/bash myprogram.sh
当它中断并且不再发出消息后,则ps
输出仅显示一行
我使用的是CentOS 6.5,带有Gnome 2.28(不幸的是,我无法升级到任何更新版本)
您是否了解可能发生的情况和/或如何进一步调查
编辑:在为真时更正
;然后
语法错误以下脚本适用于我在Linux Mint肉桂上的操作。注意“肉桂”而不是“侏儒”;如果您不确定要使用什么,请运行echo$DESKTOP\u 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 &
nohup./myprogram |$HOME/myprogram.out 2>&1&
我改变
而不是真的;然后
到,而为true;读取符号时,执行
,并将选项-r
添加到;不要运行你的代码,但是我个人会在循环内部引入一个小的睡眠。顺便说一句,外部循环也应该是,而不是真正的;不
等。添加一行*)回显“`date`Unknown”
到case语句,并以nohup myprogram.sh$HOME/myprogram.out 2>&1&
的形式运行命令。
#!/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