Comment 10 for bug 1322275

Revision history for this message
Christian Uceda (christianuceda) wrote :

For those desperate because they work for companies who take locking screen as a serious thing, this is the workaround I came with, have been running for a few weeks and it does the job well so far.

The script if run via cron every 10 mins will keep your system clean of lightdm dead sessions, your system will not get sluggish over time, this is not an ideal solution but it is a good compromise until the issue is fixed for good.

[begin file: lightdm_clean.sh]
#!/bin/bash
# Cleans lightdm session leftovers in Ubuntu 14.04.x
# Christian Uceda 2015 v0.0.2
set -u

declare PID=""
declare -i SESSION_COUNTER=0
declare -i INDICATOR_COUNTER=0
declare STATUS=""
declare SESSION=""
declare CRONMODE=""

# Send output to the console if not running on cron mode
function echo_print(){
    if [ ! "${CRONMODE}" == "yes" ]
    then
        echo -e "${1:-}"
    fi
}

# -------------------------8<--------------------- Script body

# Root required
if [ $(id -u) -ne 0 ]
then
    echo -e "\nI can only run as root.\n"
    exit 1
fi

if [ "${1:-}" == "-cron" ]
then
    CRONMODE="yes"
fi

# Get rid of lightdm "closing" sessions
while read SESSION
do
    STATUS=$(loginctl session-status "${SESSION}" | awk '$1~/State/ {print $2}')
    if [ "${STATUS}" == "closing" ]
    then
        ((SESSION_COUNTER++))
        echo_print "Teminating session: ${SESSION}"
        loginctl kill-session "${SESSION}" &>/dev/null
    fi
done < <(loginctl | awk '$3~/lightdm/ {print $1}')
echo_print "\nTerminated: ${SESSION_COUNTER} sessions.\n"

# Notify if there are still some stray indicators (if not running in cron mode) can be removed if desired.
# I used it to verify the operation did not left any stray indicators.
if [ ! "${CRONMODE}" = "yes" ]
then
    while read PID
    do
        ((INDICATOR_COUNTER++))
        if [ ! "${PID}" == "" ]
        then
            echo_print "Found indicator pid: ${PID}"
        fi
    done < <(ps aux | awk '$0~/^[l]ightdm.*indicator-services-start/ {print $2}')
    echo_print "Found: ${INDICATOR_COUNTER} stray indicators.\n"
fi

exit 0
[end of file]

To test it lock and unlock the screen a couple of times and then run it manually, run it from root's cron like:

*/10 * * * * /usr/local/sbin/lightdm_clean.sh -cron