Pause Music When Locking The Screen And Resume On Unlock For Spotify, Rhythmbox, Others

Ubuntu 18.04 Lock Screen

When you lock your computer screen (without suspending the system), most desktop audio players continue playback in the background, sometimes not emitting any sound ¹. Due to this you may unintentionally skip parts of podcasts or songs in a playlist, etc.

Enter pause-on-lock, a Bash script that pauses your music player when you lock the screen and resumes playback once the screen is unlocked.

pause-on-lock works on Unity, GNOME, Cinnamon and MATE desktop environments, and by default it supports Spotify and Rhythmbox. With the help of playerctl (a command line controller for controlling media players that support the MPRIS D-Bus interface), this script can extend its supported music players to many others, including Audacious, VLC, Cmus, and others.

To install pause-on-lock, download the script, copy it somewhere in your $PATH, like /usr/local/bin, and make it executable. E.g. install it to /usr/local/bin and automatically make it executable using (this assumes you have the pause-on-lock script available in the current folder):

sudo install pause-on-lock /usr/local/bin

Warning: Always check the code of the scripts you run on your system. The pause-on-lock script code is actually quite small, so easy to inspect.

To give it a try, all you have to do is run the script:

pause-on-lock

Now Spotify or Rhythmbox should be automatically paused when you lock your screen on MATE, GNOME, Cinnamon or Unity desktop environments, and resume upon unlocking the screen.

If you want to use pause-on-lock with other MPRIS-enabled media players, like Audacious or VLC, install playerctl:

  • Debian, Ubuntu or Linux Mint:
sudo apt install playerctl

  • Fedora:
sudo dnf install playerctl

  • Arch Linux, Manjaro:
sudo pacman -S playerctl

  • openSUSE:
sudo zypper install playerctl

  • Solus OS:
sudo eopkg install playerctl

You'll want to add pause-on-lock to your startup applications, so you don't have to run the script manually each time you want your music player to pause when locking the screen, and unpause on screen unlock. Launch Startup Application app or equivalent, and add "pause-on-lock" to your startup applications from there.

If your desktop environment / Linux distribution doesn't have a graphical tool that allows adding scripts to startup, you may also do this manually, by creating a file called pause-on-lock.desktop in ~/.config/autostart/ with the following contents:

[Desktop Entry]
Type=Application
Exec=pause-on-lock
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=pause-on-lock

¹ On Gnome 3.32 with GDM3 (Ubuntu 19.04), the default behavior is to silence the music player upon locking the screen, while it continues playback in the background. If the user then presses a key or moves the mouse so the lock screen comes on, the sound is unmuted even though the screen has not been unlocked. On Ubuntu MATE 18.04 (which uses MATE Screensaver) on the other hand, you continue to hear your music playing after locking the screen.