为什么从 ~/.profile 调用时 gdbus 输出不起作用?

为什么从 ~/.profile 调用时 gdbus 输出不起作用?

如果我打开终端并运行以下脚本,它会正常工作。调用的值gdbus会写入lockScreenTest.txt文件。屏幕锁定时值为 true,屏幕解锁时值为 false。

#!/bin/bash
while true; do 
    echo "$(gdbus call -e -d com.canonical.Unity -o /com/canonical/Unity/Session -m com.canonical.Unity.Session.IsLocked)" >> lockScreenTest.txt
    sleep 2
    echo "called" >> lockScreenTest.txt
done

但是如果我将此行添加./lockScreenCheck.sh&~/.profile并重新启动计算机,则的输出gdbus不会写入lockScreenTest.txt。相反,只会写入以下输出:

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called

called  

gdbus为什么从 运行脚本时没有可用的输出~/.profile
我正在调查这个问题,因为我编写了一个需要检查屏幕是否被锁定的 Java 程序,我大致使用以下命令来执行此操作:

String command = "gdbus call -e -d com.canonical.Unity -o /com/canonical/Unity/Session -m com.canonical.Unity.Session.IsLocked";
p = Runtime.getRuntime().exec(command);
p.waitFor(); 

与 bash 脚本一样,如果我从终端使用 运行 Java 程序,它也能正常工作java -jar program.jar,但如果我从 调用它~/.profile,Java 程序会运行,但 gdbus 命令的输出却难以捉摸。

答案1

它可能失败了,因为gdbus无法与正在运行的会话总线建立连接。

请记住,这~/.profile是由登录 shell 提供的;显示管理器可能会在登录 shell 内调用用户的桌面会话,但您不应该依赖它,即使确实如此,会话也将是 shell 的子级,而不是相反 - 环境变量不会DBUS_SESSION_BUS_ADDRESS在父 shell 中设置。

对于需要连接到桌面会话总线的应用程序,最好使用 GUI 的“启动应用程序”功能 - 例如如何在登录时自动启动应用程序?

相关内容