The goal is to mute sound whenever screen lock happens.

Ideally restore sound after unlocking as well.

Sound system is Pulseaudio.


Launch this script once when starting your X graphical session:

#!/bin/bash -euET
set -o pipefail

export DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS:-unix:path=/run/user/$(id -u)/bus}"
export DISPLAY="${DISPLAY:-:0}"

xscreensaver-command -watch | while read -r line ; do
    echo "handling event: $line"

    if [[ $line = LOCK* ]]; then
      volume=$(pamixer --get-volume)
      echo "current volume is $volume"
      pamixer --set-volume 0

    if [[ $line = UNBLANK* ]]; then
      echo "setting volume to $volume"
      pamixer --set-volume "$volume"


The "daemon" part of this script xscreensaver-command -watch will hang and wait for any events from xscreensaver. Whenever "LOCK" or "UNBLANK" event occurs, the sound volume will be zeroed and restored respectively.

  • Note that pamixer will not be installed by default on many systems, while pacmd/pactrl will be. – dirkt Sep 8 '18 at 13:25

I have found that the simplest way is a shell script which is run on startup. This should work whatever screen-saver is in use (if any).


gdbus monitor -y -d org.freedesktop.login1 | grep LockedHint --line-buffered |
    while read line
        case "$line" in
                amixer -q -D pulse sset Master off
                amixer -q -D pulse sset Master on
  • Strange, this doesn't work for me. I do get the "Session" and "Seat" events when I switch between tty2<->tty7. But I do not get anything on lock-unlock. I'm running the following command while doing the tests: gdbus monitor -y -d org.freedesktop.login1 – VasyaNovikov Sep 23 '18 at 12:50
  • That's odd, @VasyaNovikov. I get a slew of messages when I log out. Are you running Gnome3 or the Ubuntu login? – Cliff Stanford Sep 23 '18 at 14:10
  • I'm running XFCE + xscreensaver daemon. – VasyaNovikov Sep 23 '18 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.