# Session lifecycle

This chapter details the lifecycle of ThinLinc sessions, how they are created, and which aspects in the session startup and cleanup can be customized.

## Master session startup

When a user connects to a ThinLinc cluster, they are first connected to the master server.

Fig. 33 Session startup procedure — on the master

Fig. 33 shows a high-level description of what happens on the master when a ThinLinc client connects:

Check for existing sessions

1. The master checks if there are any existing sessions for the user in the master’s session database.

2. The master makes sure this information is up-to-date by asking the agent in question to verify that the sessions are still running.

Path A: Existing session

This path is taken if an existing session was found.

1. The master runs the scripts in `/opt/thinlinc/etc/sessionreconnect.d/` as the root user. See Master node scripts.

2. The master sends session information back to the client. Based on this information, the client connects directly to the agent hosting the session.

Path B: New session

This path is taken if no sessions were found.

1. The master determines which subcluster the user belongs to. Based on this, it determines which agents are candidates for hosting the user’s session.

2. The master’s load balancer picks the agent best suited to host the session from the list of candidates.

3. The master contacts the chosen agent, requesting a new graphical session for the user.

4. The master runs the scripts in `/opt/thinlinc/etc/sessionstartup.d/` as the root user. See Master node scripts.

5. The master sends session information back to the client. Based on this information, the client connects directly to the agent hosting the session.

## Agent session startup

Fig. 34 Session startup procedure — on the agent

Fig. 34 outlines the flow after the master requests a new session to be created on the agent:

Check for user-configured session startup

The agent selected to host the session first executes the script `/opt/thinlinc/etc/xsession`. If `~/.thinlinc/xstartup` is not present in the user’s home directory, Path A: Default session startup will be taken. If present, Path B: User-defined session startup will be taken.

Note

If it is undesirable to leave control over the session startup to to the end-user, the shell script `/opt/thinlinc/etc/xsession` can be modified so it does not execute `~/.thinlinc/xstartup`.

Any changes to `/opt/thinlinc/etc/xsession` must be reapplied after the ThinLinc server has been upgraded.

Path A: Default session startup

This path is taken if `~/.thinlinc/xstartup` was not found. The agent then runs the script `/opt/thinlinc/etc/xstartup.default`:

Fig. 35 outlines the high-level flow of what happens then:

Fig. 35 The ThinLinc profiles and `xstartup.default`

1. All files in `/opt/thinlinc/etc/xstartup.d/` are executed. See Agent node scripts.

3. When all scripts in `xstartup.d/` have been executed, the profile specified by the environment variable TLPROFILE is run. This environment variable is typically set by the profile chooser (`xstartup.d/20-tl-select-profile.sh`).

   For example, if TLPROFILE is set to mate, the profile configured in `/profiles/mate` will be run.

4. Once the profile has exited, typically by the user logging out, the scripts in `/opt/thinlinc/etc/xlogout.d/` are run. See Agent node scripts.

Path B: User-defined session startup

This path is taken if `~/.thinlinc/xstartup` was found. In this case, that file is executed instead of `/opt/thinlinc/etc/xstartup.default`.

The `~/.thinlinc/xstartup` script is conceptually similar to the `~/.xinitrc` commonly used for local graphical sessions.