How Do You Keep a Terminal Session Alive?

9

The system or server administrator often needs to keep a terminal session alive for maintaining a process or application running. However, when we close the SSH connection or do not send any information to the server, the terminal session will close. To prevent the server to auto-close the terminal session, we can perform some configuration. This post will provide two ways to keep a terminal session alive:

  1. By setting up the configuration file
  2. Using Screen

Well, there is a quick way to prevent the server from closing the session. We can simply add the following parameter to the SSH command and log in to the server.

$ ssh -o ServerAliveInterval=60 user>@ip>

The router will send information every 60 seconds to keep the server session alive by running the above command.

But, if you do not want to set it every time while logging in, you can select the default configuration in the configuration file of SSH.

Now, let’s see how we can set up the configuration files to keep the terminal session alive.

Keep a Terminal Session Alive by Setting Up the Configuration File

To edit the configuration file of SSH, type the command given below:

$ sudo nano $HOME/.ssh/config

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645476 9 How Do You Keep a Terminal Session Alive

The above command will create the configuration file and open the file in the nano editor to let you edit it even if it does not exist.

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645477 311 How Do You Keep a Terminal Session Alive

After opening it in a nano editor, add the content provided below in the configuration file:

Host *
ServerAliveInterval 60

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645477 511 How Do You Keep a Terminal Session Alive

The above configuration will keep on sending the alive signal after every 60 seconds for all the hosts.

After having this configuration, we must change the file’s mod using chmod to grant read and writable permissions. To do so, type the command provided below:

$ chmod 600 $HOME/.ssh/config

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645478 871 How Do You Keep a Terminal Session Alive

When you log in to the server, the terminal session won’t automatically close. Instead, the configuration file will keep sending the alive signal after the specific interval set in the configuration file to keep the terminal session alive.

Here arises another problem. What if some processes need to run for hours. Does the client machine also need to stay ON to keep sending the alive signal to the server?

Well, in the above-provided method, yes. The client machine has to stay on. But, there is another way to keep the terminal session alive.

Let’s learn a better and more efficient way to keep the terminal session alive.

Keep a Terminal Session Alive Using Screen

The Screen is an application used to keep the terminal session alive even when the client machine is rebooted or turned off. This application can be used to keep the terminal session of the server alive for hours until the process running in the session is completed.

Let’s jump into the process and see how to install and use the screen to keep a terminal session alive.

Installation of Screen

First of all, login to the server using SSH:

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645479 876 How Do You Keep a Terminal Session Alive

After logging into the server’s machine, update the server system’s repository cache:

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645479 485 How Do You Keep a Terminal Session Alive

And run the installation command of the screen using the command provided below:

$ sudo apt install screen

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645480 745 How Do You Keep a Terminal Session Alive

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645480 824 How Do You Keep a Terminal Session Alive

The installation of the screen can be verified using the command provided below:

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645481 888 How Do You Keep a Terminal Session Alive

Now, when the screen application is installed on the server. The usage is straightforward.

Usage of Screen

To create a new session using the screen, you can simply run the “screen” command to start a new session.

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645481 996 How Do You Keep a Terminal Session Alive

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645482 151 How Do You Keep a Terminal Session Alive

You can also provide a name to the session while creating a new session, as shown below:

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645482 906 How Do You Keep a Terminal Session Alive

The above commands will create a new session in which you can run or start any process of your choice.

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645483 498 How Do You Keep a Terminal Session Alive

For example, we want to upgrade the system’s installed packages.

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645484 623 How Do You Keep a Terminal Session Alive

Now, while it is running, you can detach from the session using the keyboard shortcut keys CTRL+A+D, and all the processes will keep on working behind the scene even if you log out from the server using SSH.

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645484 632 How Do You Keep a Terminal Session Alive

To connect back to the session, log back into the server using SSH again if you are logged out and use the command given below:

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645485 308 How Do You Keep a Terminal Session Alive

But if there are multiple sessions, you need to type the session ID, and you can get the session ID using the following command:

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645485 743 How Do You Keep a Terminal Session Alive

From the provided list of sessions, choose the session to which you want to join, and give its session ID as shown below:

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645485 665 How Do You Keep a Terminal Session Alive

how do you keep a terminal session alive? How Do You Keep a Terminal Session Alive? 1623645486 416 How Do You Keep a Terminal Session Alive

This method is how you can install and use the screen to keep the terminal session alive for hours until a process is finished.

Conclusion

This post is all about how to keep a terminal session alive. It contains a brief knowledge about the configuration of SSH for sending alive signals after a specific interval of time. This post also provides a detailed method on how to install and use the screen application to keep the terminal session alive.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More