Prolonging terminal sessions: Difference between revisions

From Alliance Doc
Jump to navigation Jump to search
(swap order of tmux and screen, based on staff popularity)
No edit summary
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Draft}}
<languages />
<translate>
<!--T:1-->
To work on our clusters, most users will need to use [[SSH]] to connect to the cluster for job submission and monitoring, editing files and so forth. Keeping this SSH connection alive for a long period of time, ranging from hours to days, may be necessary for some users and this page proposes a variety of techniques for keeping such a terminal session alive.


For working on the clusters, most users will need to use [[SSH]] to connect to the cluster for job submission and monitoring, editing files and so forth. Keeping this SSH connection alive for a long period of time, ranging from hours to days, may be necessary for some users and this page proposes a variety of techniques for keeping such a terminal session alive.
=SSH configuration= <!--T:2-->


=SSH configuration=
<!--T:3-->
 
One simple solution is to modify the configuration of your SSH client to prolong the connection. On MacOS and Linux the client configuration is found in <code>$HOME/.ssh/config</code> while in Windows it is located in <code>C:\Users\<username>\.ssh\config</code>. Note that the file may not exist initially, so you will need to create it; you should add the lines  
One simple solution is to modify the configuration of your SSH client to prolong the connection. On MacOS and Linux the client configuration is found in <tt>$HOME/.ssh/config</tt> while in Windows it is located in <tt>C:\Users\<username>\.ssh\config</tt>. Note that the file may not exist initially, so you will need to create it; you should add the lines  
</translate>
<source>
<source>
Host *
Host *
     ServerAliveInterval 240
     ServerAliveInterval 240
</source>
</source>
This addition will ensure the transmission of a sign-of-life signal over the SSH connection to any remote server, including Alliance clusters every 240 seconds, i.e. four minutes, which should help to keep your SSH connection alive even if it is idle for several hours.
<translate>
<!--T:4-->
This addition will ensure the transmission of a sign-of-life signal over the SSH connection to any remote server (such as an Alliance cluster) every 240 seconds, i.e. four minutes, which should help to keep your SSH connection alive even if it is idle for several hours.


=Terminal multiplexers=
=Terminal multiplexers= <!--T:5-->


The programs <tt>tmux</tt> and <tt>screen</tt> are examples of a terminal multiplexer - a program which allows you to detach your terminal session entirely, where it will keep on running on its own until you choose to reattach to it. With such a program, you can logout from the cluster, turn off the workstation or hibernate the laptop you use to connect to the cluster and when you're ready to start working again the next day, reattach to your session and start from right where you left off.  
<!--T:6-->
{{box|'''Login Node Dependency'''
The programs <code>tmux</code> and <code>screen</code> are examples of a terminal multiplexer—a program which allows you to detach your terminal session entirely, where it will keep on running on its own until you choose to reattach to it. With such a program, you can log out from the cluster, turn off the workstation or hibernate the laptop you use to connect to the cluster and when you're ready to start working again the next day, reattach to your session and start from right where you left off.  
{{box|<b>Login node dependency</b>
<br>
<br>
Each of our clusters has several login nodes and your <tt>tmux</tt> or <tt>screen</tt> session is specific to a login node. If you wish to reattach to a session, you must ensure you're connected to the right login node which of course means remembering which login node you were using when you started <tt>tmux</tt> or <tt>screen</tt>. Login nodes may also occasionally be rebooted, which will kill any detached terminal sessions on that node.}}   
Each of our clusters has several login nodes and your <code>tmux</code> or <code>screen</code> session is specific to a login node. If you wish to reattach to a session, you must ensure you're connected to the right login node, which of course means remembering which login node you were using when you started <code>tmux</code> or <code>screen</code>. Login nodes may also occasionally be rebooted, which will kill any detached terminal sessions on that node.}}   


==tmux==
==tmux== <!--T:7-->


<!--T:8-->
The [https://en.wikipedia.org/wiki/Tmux tmux] software is a terminal multiplexer, allowing multiple virtual sessions in a single terminal session. You can thus disconnect from an SSH session without interrupting its process(es).
The [https://en.wikipedia.org/wiki/Tmux tmux] software is a terminal multiplexer, allowing multiple virtual sessions in a single terminal session. You can thus disconnect from an SSH session without interrupting its process(es).


===Cheat sheet===
<!--T:9-->
Here are some introductions to tmux:
* [https://leanpub.com/the-tao-of-tmux/read "The Tao of tmux"], an online book
* [https://www.youtube.com/watch?v=252K9lrRdMU "Getting Started With TMUX"], a 24-minute video
* [https://www.youtube.com/watch?v=Y1Of3S5iVog "Turbo boost your interactive experience on the cluster with tmux"], a 58-minute video
 
===Cheat sheet=== <!--T:10-->


<!--T:11-->
For a complete reference, see [http://hyperpolyglot.org/multiplexers this page].
For a complete reference, see [http://hyperpolyglot.org/multiplexers this page].


<!--T:12-->
{| class="wikitable"
{| class="wikitable"
|-
|-
! Command !! Description
! Command !! Description
|-
|-
| <tt>tmux</tt> || Start a server
| <code>tmux</code> || Start a server
|-
|-
| <tt>Ctrl+B D</tt> ||  Disconnect from server
| <code>Ctrl+B D</code> ||  Disconnect from server
|-
|-
| <tt>tmux a</tt> || Reconnect to server
| <code>tmux a</code> || Reconnect to server
|-
|-
| <tt>Ctrl+B C</tt> || Create a new window
| <code>Ctrl+B C</code> || Create a new window
|-
|-
| <tt>Ctrl+B N</tt> || Go to next window
| <code>Ctrl+B N</code> || Go to next window
|-
|-
| <tt>Ctrl+B [</tt> || Enable "copy" mode, allowing to scroll with the mouse and Page-Up Page-Down
| <code>Ctrl+B [</code> || Enable "copy" mode, allowing to scroll with the mouse and Page-Up Page-Down
|-
|-
| <tt>Esc</tt> || Disable "copy" mode  
| <code>Esc</code> || Disable "copy" mode  
|}
|}


===Launch tmux inside a job submitted through tmux===  
===Launch tmux inside a job submitted through tmux=== <!--T:13-->


If you submit a job with tmux and try to start tmux within the same job, you will get the <tt>lost server</tt> error message. This happens because the <tt>$TMUX</tt> environment variable pointing to the tmux server is propagated to the job. The value of the variable is not valid and you can reset it with: {{Command|unset TMUX}}
<!--T:14-->
If you submit a job with tmux and try to start tmux within the same job, you will get the <code>lost server</code> error message. This happens because the <code>$TMUX</code> environment variable pointing to the tmux server is propagated to the job. The value of the variable is not valid and you can reset it with: {{Command|unset TMUX}}


However, nested use of tmux is not recommended. To send commands to a nested tmux, one has to hit <tt>Ctrl+B</tt> twice; for example, to create a new window, one has to use <tt>Ctrl+B Ctrl+B C</tt>. Consider using [[#GNU Screen | screen]] inside your job (if you are using tmux on a login node).
<!--T:15-->
However, nested use of tmux is not recommended. To send commands to a nested tmux, one has to hit <code>Ctrl+B</code> twice; for example, to create a new window, one has to use <code>Ctrl+B Ctrl+B C</code>. Consider using [[#GNU Screen | GNU Screen]] inside your job (if you are using tmux on a login node).


==GNU Screen==
==GNU Screen== <!--T:16-->


The [https://en.wikipedia.org/wiki/GNU_Screen screen] program is a widely used terminal multiplexer. To create a detached terminal session, you can use the following command
<!--T:17-->
The [https://en.wikipedia.org/wiki/GNU_Screen GNU Screen] program is another widely used terminal multiplexer. To create a detached terminal session, you can use the following command
{{Command|screen -S <session name>}}
{{Command|screen -S <session name>}}
It's a good idea to give a descriptive name to your terminal sessions, making it easier to identify them later. You can use the command <tt>screen -list</tt> to see a list of your detached terminal sessions on this node,
It's a good idea to give a descriptive name to your terminal sessions, making it easier to identify them later. You can use the command <code>screen -list</code> to see a list of your detached terminal sessions on this node,
{{Command
{{Command
|screen -list
|screen -list
Line 64: Line 82:
1 Socket in /tmp/S-stubbsda.
1 Socket in /tmp/S-stubbsda.
}}
}}
You can attach to one of your sessions using the command <tt>screen -d -r <session name></tt>.
You can attach to one of your sessions using the command <code>screen -d -r <session name></code>.
</translate>

Latest revision as of 16:39, 4 July 2023

Other languages:

To work on our clusters, most users will need to use SSH to connect to the cluster for job submission and monitoring, editing files and so forth. Keeping this SSH connection alive for a long period of time, ranging from hours to days, may be necessary for some users and this page proposes a variety of techniques for keeping such a terminal session alive.

SSH configuration

One simple solution is to modify the configuration of your SSH client to prolong the connection. On MacOS and Linux the client configuration is found in $HOME/.ssh/config while in Windows it is located in C:\Users\<username>\.ssh\config. Note that the file may not exist initially, so you will need to create it; you should add the lines

Host *
    ServerAliveInterval 240

This addition will ensure the transmission of a sign-of-life signal over the SSH connection to any remote server (such as an Alliance cluster) every 240 seconds, i.e. four minutes, which should help to keep your SSH connection alive even if it is idle for several hours.

Terminal multiplexers

The programs tmux and screen are examples of a terminal multiplexer—a program which allows you to detach your terminal session entirely, where it will keep on running on its own until you choose to reattach to it. With such a program, you can log out from the cluster, turn off the workstation or hibernate the laptop you use to connect to the cluster and when you're ready to start working again the next day, reattach to your session and start from right where you left off.

Login node dependency
Each of our clusters has several login nodes and your tmux or screen session is specific to a login node. If you wish to reattach to a session, you must ensure you're connected to the right login node, which of course means remembering which login node you were using when you started tmux or screen. Login nodes may also occasionally be rebooted, which will kill any detached terminal sessions on that node.

tmux

The tmux software is a terminal multiplexer, allowing multiple virtual sessions in a single terminal session. You can thus disconnect from an SSH session without interrupting its process(es).

Here are some introductions to tmux:

Cheat sheet

For a complete reference, see this page.

Command Description
tmux Start a server
Ctrl+B D Disconnect from server
tmux a Reconnect to server
Ctrl+B C Create a new window
Ctrl+B N Go to next window
Ctrl+B [ Enable "copy" mode, allowing to scroll with the mouse and Page-Up Page-Down
Esc Disable "copy" mode

Launch tmux inside a job submitted through tmux

If you submit a job with tmux and try to start tmux within the same job, you will get the lost server error message. This happens because the $TMUX environment variable pointing to the tmux server is propagated to the job. The value of the variable is not valid and you can reset it with:

Question.png
[name@server ~]$ unset TMUX

However, nested use of tmux is not recommended. To send commands to a nested tmux, one has to hit Ctrl+B twice; for example, to create a new window, one has to use Ctrl+B Ctrl+B C. Consider using GNU Screen inside your job (if you are using tmux on a login node).

GNU Screen

The GNU Screen program is another widely used terminal multiplexer. To create a detached terminal session, you can use the following command

Question.png
[name@server ~]$ screen -S <session name>

It's a good idea to give a descriptive name to your terminal sessions, making it easier to identify them later. You can use the command screen -list to see a list of your detached terminal sessions on this node,

Question.png
[name@server ~]$ screen -list
There is a screen on:
        164133.foo      (Attached)
1 Socket in /tmp/S-stubbsda.

You can attach to one of your sessions using the command screen -d -r <session name>.