If I open a terminal and run the below script, it works fine. The values from the gdbus
call get written to the lockScreenTest.txt
file. The values are true when the screen is locked and the values are false when the screen is unlocked.
#!/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
But if I add this line ./lockScreenCheck.sh&
to ~/.profile
and restart the computer, the output of gdbus
doesn't get written to lockScreenTest.txt
. Instead, only the following output is written:
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
Why isn't the output of gdbus
available when the script is run from ~/.profile
?
I'm investigating this because I wrote a Java program which needs to check if the screen is locked, and I'm doing it with roughly the following commands:
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();
Like the bash script, the Java program also works fine if I run it from a terminal with java -jar program.jar
, but if I invoke it from ~/.profile
, the Java program runs, but the output from the gdbus command is elusive.
It's probably failing because
gdbus
cannot establish a connection to a running session bus.Remember that
~/.profile
is sourced by login shells; it may be the case that the display manager invokes a user's desktop session inside a login shell, but you shouldn't count on it and even if it does, the session will be a child of the shell rather than the other way around - environment variables such asDBUS_SESSION_BUS_ADDRESS
won't be set in the parent shell.For applications that need to connect to a desktop session's bus, you are better off using the GUI's "Startup Applications" feature - see for example How do I start applications automatically on login?