如何创建一个守护进程来监听 dbus 并在消息上触发脚本

如何创建一个守护进程来监听 dbus 并在消息上触发脚本

我想创建一个守护进程,每次我解锁 ubuntu 上的屏幕时,它都会在后台启动 shell 脚本。我设法根据相关问题的答案创建这样的脚本:在屏幕上运行脚本锁定解锁。它在终端窗口中运行良好。但现在我想从中创建一个守护进程,但我还没有得到任何运气。
有什么建议么?

答案1

基于https://askubuntu.com/questions/150790/how-do-i-run-a-script-on-a-dbus-signal

#!/bin/bash

interface=org.gnome.ScreenSaver
member=ActiveChanged

dbus-monitor --profile "interface='$interface',member='$member'" |
while read -r line; do
    echo $line | grep ActiveChanged && your_script_goes_here
done

只要把它插进去/etc/init.d/monitor-for-unlock,使其可执行,然后在rc2.d中进行软链接

chmod +x /etc/init.d/monitor-for-unlock
cd /etc/rc2.d
ln -s /etc/init.d/monitor-for-unlock .

答案2

系统中已经有这样一个守护进程 - upstart,你只需要为它做一个会话作业即可。

description "some job description"
start on desktop-unlock
script
        /path/to/your/executable
end script

相关内容