Question: i love screen utility and i use it extensively on my server so i set up my .bashrc file to resume my screen session on login. the only function i am missing is something that logs out from the ssh session without the need of detaching/closing the screen session explicitly.

i was thinking about some script that would run `screen -dS "mainScreen"; exit` but it is not possible as this script obviously continues its execution inside the screen session after the detach instruction and does not affect my ssh session, so the only thing i get is that the screen session is terminated.

is there a way to do the 'detatch and exit' action atomically leading the screen to continue running and my ssh session to terminate?

Answer: 
ssh supports a mechanism by which you can enter input directly to it instead of to the shell on the other end of the connection. That mechanism is enabled when you type the escape key, which can be set with `-e` and defaults to `~`. This is useful for various functions like setting up port forwarding in an already connected session, or terminating the connection. You can type `~?` to get a complete list of available commands. In particular, to terminate the session, type:

```
~.

```
Instead of having `ssh` run a login shell (the default), have it run `screen` directly:

```
ssh remoteHost -t screen -dRS "mainScreen"

```

When you detach, the screen client exits, which causes `ssh` to close the connection.

(Note: I include the `screen` command suggested by Steve, but you can use whatever command you would ordinarily use to connect to your session.)