我正在尝试在屏幕锁定/解锁时执行 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.5 和 Gnome 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/