How to run scripts on session lock/unlock (kde plasma 6)?

Hello,

As the title says, I would like to run a script when I unlock my session (NOT ONLY when I login !)
I know it used to be possible thanks to an option that was removed. Is there any alternative way of doing this ? I couldn’t find any.

FYI I’m using KDE Plasma 6.2.5 with Wayland on Arch Linux

1 Like

Hi! I’m no expert here, but some of the answers given at this link - Run script on screen lock in KDE - Unix & Linux Stack Exchange - seem like they would be applicable still.

At least on my Plasma 6.2.5 system running Wayland, I can verify by running dbus-monitor --session "type='signal',interface='org.freedesktop.ScreenSaver'" that the interface does still send two messages back-to-back setting the member ActiveChanged to false when I unlock my screen, so that might be a viable path :slight_smile:

It does work, thank you !

I made a systemd service, if anyone finds this and is interested, in the script that the service runs, you need to set the following environment variable:
export DBUS_SESSION_BUS_ADDRESS=“unix:path=/run/user/1000/bus”

1 Like

I’ve been working my brain trying to figure out how to do this as well, any chance you could share your systemd service for this? I haven’t exactly been able to get it working myself so far.

Yeah sure

systemd service “YourService.service” :

# ======== Setup ========
# 1. Copy the file/socket printed into PacmanNotifyUpdates.sh
#    > echo $DBUS_SESSION_BUS_ADDRESS
#    => Example: if you got "unix:path=/run/user/1000/bus", replace with:
#    => BUS_FILE_ADDRESS="/run/user/1000/bus"
# 2. In this service file, replace "User=your_username" by your username
# ======== Install ========
# > sudo mkdir -p /usr/local/bin/YourService.d
# > sudo cp *.sh /usr/local/bin/YourService.d
# > sudo chown your_user /usr/local/bin/YourService.d/*
# > chmod 550 /usr/local/bin/YourService.d/*
# > sudo cp ThisService.service /etc/systemd/system
# > sudo systemctl enable --now YourService.service

[Unit]
Description=Pacman updates notification daemon
StartLimitIntervalSec=300
StartLimitBurst=5

[Service]
WorkingDirectory=/usr/local/bin/PacmanNotifyUpdates.d
ExecStart=bash Service.sh
Restart=always
RestartSec=30
User=your_username

[Install]
WantedBy=graphical.target

Service.sh

#!/bin/bash

BUS_FILE_ADDRESS="/run/user/1000/bus"
export DBUS_SESSION_BUS_ADDRESS="unix:path=$BUS_FILE_ADDRESS"
set -e

# Note: we cannot use -f because it is a socket file,
# and apparenlty -f doesn't work with sockets
while [ ! -e $BUS_FILE_ADDRESS ]; do
	sleep 1
done

echo "User session started, now listening for events"

dbus-monitor --session "type='signal',interface='org.freedesktop.ScreenSaver'" |
while read x; do
	case "$x" in 
                # Do whatever you want here
		# *"boolean true"*) echo SCREEN_LOCKED && notify-send SCREEN_LOCKED;;
		*"boolean false"*) bash ./HandleUnlockEvent.sh;;
	esac
done