How do I call `dbus` code that monitors when screen is locked/unlocked?
I have a code snippet from (unix.stackexchange.com - Run script on screen lock / unlock) which I plan to modify because PulseAudio "undocumented feature" switches sound from TV to laptop when screen is locked.
The code is pretty straight forward:
dbus-monitor --session "type='signal',interface='com.ubuntu.Upstart0_6'" | \
(
while true; do
read X
if echo $X | grep "desktop-lock" &> /dev/null; then
SCREEN_LOCKED;
elif echo $X | grep "desktop-unlock" &> /dev/null; then
SCREEN_UNLOCKED;
fi
done
)
I can't really say I understand the program / subroutine top-down flow or looping but someone from here commented there that it works and I trust his judgement.
The question is what are the naming conventions for my script? What is the industry standard directory to put the script in? How do I invoke it? ie Startup applications, rc.local, cron @reboot, etc. After invocation I trust it runs until the next reboot.
It will be running forever even if it's only used every Wednesday Laundry night so ideally it shouldn't hog too many CPU cycles.
The script
As mentioned in comments, a slightly improved version is posted here (with the help of @Serg and @muru):
dbus-monitor --session "type='signal',interface='com.ubuntu.Upstart0_6'" | \
(
while read X; do
if [[ "$X" =~ desktop-lock ]]; then
SCREEN_LOCKED;
elif [[ "$X" =~ desktop-unlock ]]; then
SCREEN_UNLOCKED;
fi
done
)
Where to store it?
You can store it anywhere you like in your $HOME
directory if it is for your user only, or in /usr/local/bin if you want it to be available for other users.
When to run?
dbus-monitor runs locally. I would therefore simply add the script to Startup Applications: Dash > Startup Applications > Add. Add the command to run the script, best make it executable, and subsequently simply add the command:
/path/to/script
Possibly, since the script involves the GUI, you need to wait until the UI is fully loaded. In that case, use:
/bin/bash -c "sleep 10 && /path/to/script"
Note
All the script does is wait for the state to change, listening to the communication between processes on your system. It does not add any noticeable burden to your system.
This worked for me for Ubuntu 18.04 with Unity
#!/bin/bash
dbus-monitor --session "type='signal',interface='com.canonical.Unity.Session'" | \
(
# optional: prevent consecutive unlocks/locks
locked=0
while true; do
read X
echo "$X"
if echo "$X" | grep "member=Locked" &> /dev/null; then
if [ $locked -eq 0 ]; then
echo "Screen locked"
locked=1
fi
elif echo "$X" | grep "member=Unlocked" &> /dev/null; then
if [ $locked -eq 1 ]; then
echo "Screen unlocked"
locked=0
fi
fi
done
)
When using Gnome the second line should be replaced with
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | \
and the content between the quotes after grep with "boolean true" and "boolean false" respectively.
Add this script to the startup applications and it will start to work once you login for the first time.