Skip to content

Running a user logout script

matt335672 edited this page Aug 14, 2020 · 1 revision

Introduction

Some deployments may find it useful to run a script when a user logs out from an XRDP session.

One way to do this, is to take the startwm.sh script provided by your distribution and modify it so that the logout script is called when the window manager process exits.

Example - Xubuntu 20.04

This distribution provides XRDP version 0.9.12-1.

A default XRDP installation has a /etc/xrdp/startwm.sh script which does the following:-

  • Sets up the process environment from /etc/profile
  • Sets up locale-specific environment variables
  • execs /etc/X11/xsession to start the desktop environment.

There are at least three ways to get this script to call a user logout script.

  1. Modify the distribution-provided startwm.sh to call the script.
  2. Take a copy of startwm.sh and modify that. Then get sesman to call the modified script.
  3. Write a totally separate script (called by sesman) which calls the original script to implement some of the required functionality.

This description uses the second method. You may prefer to use one of the others, or something different.

Requirements

  • A global script /usr/local/bin/xrdp_logout is called when a user logs out.
  • Optionally, the user can supply an override for this script called .xrdp_logout.
  • The logout process should be loggable.

Implementation

The original script is written in Bourne shell using a particular style. The modifications we are making here are in the same style. You may prefer to use bash instead if that suits your environment better.

  • Copy /etc/xrdp/startwm.sh to /etc/xrdp/startwm_and_logout.sh, making sure it's executable:-
    $ sudo cp -p /etc/xrdp/startwm.sh /etc/xrdp/startwm_and_logout.sh
  • Edit /etc/xrdp/startwm_and_logout.sh. The last two lines look like this:-
    test -x /etc/X11/Xsession && exec /etc/X11/Xsession
    exec /bin/sh /etc/X11/Xsession
  • Remove these two lines and replace them with the following code. This works the same as the code it's replacing, except that control returns to the calling script when the session finishes.
    if test -x /etc/X11/Xsession; then
            /etc/X11/Xsession
    else
            /bin/sh /etc/X11/Xsession
    fi
    Then append the extra functionality to the end of the script:-
    USER_LOGOUT=$HOME/.xrdp_logout
    SYSTEM_LOGOUT=/usr/local/bin/xrdp_logout
    
    # Set up a log file if the data home directory is available
    test -z "$XDG_DATA_HOME" && XDG_DATA_HOME=$HOME/.local/share
    if test -d "$XDG_DATA_HOME"; then
            exec >"$XDG_DATA_HOME/xrdp/xrdp_logout_$DISPLAY.log" 2>&1
    fi
    
    if test -r $USER_LOGOUT; then
            if test -x $USER_LOGOUT; then
                    $USER_LOGOUT
            else
                    /bin/sh $USER_LOGOUT
            fi
    
    elif test -r $SYSTEM_LOGOUT; then
            if test -x $SYSTEM_LOGOUT; then
                    $SYSTEM_LOGOUT
            else
                    /bin/sh $SYSTEM_LOGOUT
            fi
    fi
    
    exit 0
  • Edit /etc/xrdp/sesman.ini. Set DefaultWindowManager=startwm_and_logout.sh
  • sudo systemctl restart xrdp-sesman
Clone this wiki locally