6

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.

  • Hi win, there is an improved version here: http://askubuntu.com/questions/854745/how-to-make-a here muru and serg had a few suggestions). Replace the capitalized sections with commands and add it to startup applications, possibly a little break is needed. I would answer, but am on mobile :) I will if no one answers in between. The script is no burden to your system. – Jacob Vlijm Dec 08 '16 at 08:15
  • @JacobVlijm Thanks for link with improved version of code. After I figure out a script name and a directory name I'll paste that in. – WinEunuuchs2Unix Dec 08 '16 at 11:18
  • @JacobVlijm closest answer so far is to put scripts in init.d and with symbolic link to rc.2 here: http://unix.stackexchange.com/questions/80143/how-to-create-a-daemon-which-would-be-listening-to-dbus-and-fire-script-on-messa Kind of fascinating there isn't an answer in google so far (from my searches at least). – WinEunuuchs2Unix Dec 09 '16 at 02:01
  • Hi WinEunuuchs2Unix, posted :) – Jacob Vlijm Dec 09 '16 at 08:06

2 Answers2

3

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.

1mi
  • 409
3

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.

Jacob Vlijm
  • 83,767
  • Instant +1 for answer. Thank you. I might set it up as a daemon as described here: http://unix.stackexchange.com/questions/80143/how-to-create-a-daemon-which-would-be-listening-to-dbus-and-fire-script-on-messa but I will time this weekend to play with it before selecting the best path. – WinEunuuchs2Unix Dec 09 '16 at 11:39
  • doesn't seem to work on ubuntu 17.04. The ouput of dbus-monitor --session "type='signal',interface='com.ubuntu.Upstart0_6'" is signal time=1497336035.520628 sender=org.freedesktop.DBus -> destination=:1.140 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired string ":1.140" signal time=1497336035.520706 sender=org.freedesktop.DBus -> destination=:1.140 serial=4 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameLost string ":1.140", then nothing when I lock or unlock – maxbellec Jun 13 '17 at 06:41