Remember applications on logout from LXDE

lxdesessionx11

When I start my computer I want that remember my applications opened before close the last session such as Ubuntu, and other distros do normally.

Googling, I see that I can run apps when I start session, configuring the autostart file but I don't want always run the same programs at start, instead I want reopen the programs opened before close last session.

I use Fedora spin with LXDE.

Best Answer

Apparently LXDE doesn't have a proper session manager. However as you've mentioned we can use the autostart file. All we need to do is create a dynamic list of the programs we are running before we exit the desktop.

Here is a little bash script I whipped up that will parse the children of the root X11 window looking for apps to add to the autostart file. It has optional black and white lists. Use the blacklist for things like the window manager or anything you never want to run. Conversely use the whitelist for something you always want to run.

Try running it in a shell to see what the output looks like, then you can see if you need to add anything to the blacklist.

You'll need to wire this up to run somehow before you exit LXDE. Probably by adding a new entry into a menu.

For example: scriptnamehere.bash > ~/.config/lxsession/LXDE/autostart

As you can see this will rewrite the autostart file each time it is run, hence the need for the whitelist.

#!/bin/bash

WINDOWS=($(xwininfo -root -children | \
    egrep \"[a-zA-Z]*\" | \
    cut -d' ' -f7 | \
    sed 's/":\?//g' | \
    sort -du))

BLACKLIST=()
WHITELIST=()

for window in ${WINDOWS[@]}; do
    # test to see if element in array is an executable
    WHICH=$(which $window 2>/dev/null)

    # is it in the blacklist?
    # if so, move to the next window
    for app in ${BLACKLIST[@]}; do
    if [[ $app == $window ]]; then
        continue 2
    fi
    done

    # otherwise add it to the autostart list
    if [[ -f $WHICH ]]; then
        APPS[${#APPS[*]}]="$window"
    fi
done

# add whitelisted apps
for app in ${WHITELIST[@]}; do
    APPS[${#APPS[*]}]="$app"
done

for app in ${APPS[@]}; do
    printf "@%s\n" "$app"
done
Related Question