Description
I have found out that each new remote desktop spawns a "ssh-agent -s" and a system-config-printer "applet.py" process which will remain once the desktop session is closed, preventing the systemd session from exiting normally.
This happens at least when using lightdm, I haven't tried with other display managers.
The problem with applet.py is known upstream and is WONTFIX. It's been reported to happen with sddm, kdm and lightdm.
I propose the following solution for lightdm:
-
Edit /etc/lightdm/lightdm.conf and put this line under section [Seat:*]
session-cleanup-script=/etc/lightdm/session-cleanup-script.sh
-
Create the file /etc/lightdm/session-cleanup-script.sh with the following content
#!/bin/bash
#exec >/tmp/lightdm-cleanup-script.log 2>&1 #uncomment this line to log any error output when the script is run
# kill lingering applet.py and "ssh-agent -s" processes
ALL_SESSION_ID=$(loginctl list-sessions|cut -c -7|grep -E "[0-9]+$")
for SESSION_ID in $ALL_SESSION_ID; do
SESSION_SCOPE=$(systemd-cgls --no-pager -u session-$SESSION_ID.scope)
LEADER_PID=$(loginctl -p Leader --value show-session $SESSION_ID)
# get the pids using grep -noE to extract strings of numbers prepended by line_number':'. There can be more than one hit per line so use uniq to keep just the first result. uniq comparison works only up to 99 lines.
SESSION_PIDS=$(echo "$SESSION_SCOPE"|grep -noE "[1-9][0-9]*"|uniq -w 2|cut -f 2 -d ':')
# if leader PID doesn't exist anymore
if [[ ! " ${SESSION_PIDS} " =~ [[:space:]]${LEADER_PID}[[:space:]] ]]; then
PIDS=$(echo "$SESSION_SCOPE"|grep -E "(applet.py)|(ssh-agent -s)"|grep -noE "[1-9][0-9]*"|uniq -w 2|cut -f 2 -d ':')
# kill processes
for PID in $PIDS; do kill $PID; done
fi
done
-
Make the script executable with
chmod +x /etc/lightdm/session-cleanup-script.sh
-
Restart lightdm
systemctl restart lightdm
Explanation: I haven't managed to get the current systemd session scope for the lightdm process launching the session-cleanup script ("loginctl session-status" returns error), so I resorted to kill all "ssh-agent -s" and "applet.py" processes on all systemd session scopes where the leader has already been killed.
This script will be run each time the desktop session is terminated.
A bit blunt but works fine.