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.
~/.profile
being sourced? if it's outside of a desktop session, then likelyDBUS_SESSION_BUS_ADDRESS
has not yet been setDBUS_SESSION_BUS_ADDRESS
just now but couldn't get much of a clue as to what the problem might be.~/.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 you certainly can't count on the DBUS being active before the session. I'd suggest moving yourlockScreenCheck
code to a Startup Application - see for example How do I start applications automatically on login?