How Do You Keep a Terminal Session Alive?
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:
- By setting up the configuration file
- 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
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.
After opening it in a nano editor, add the content provided below in the configuration file:
Host *
ServerAliveInterval 60
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
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:
After logging into the server’s machine, update the server system’s repository cache:
And run the installation command of the screen using the command provided below:
$ sudo apt install screen
The installation of the screen can be verified using the command provided below:
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.
You can also provide a name to the session while creating a new session, as shown below:
The above commands will create a new session in which you can run or start any process of your choice.
For example, we want to upgrade the system’s installed packages.
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.
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:
But if there are multiple sessions, you need to type the session ID, and you can get the session ID using the following command:
From the provided list of sessions, choose the session to which you want to join, and give its session ID as shown below:
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.