SSH provides a secured emulation of server terminal on client computer. As we know if the terminal attached to processes is closed then the processes are killed. This could be problem when we are having a internet connection drops. In this article we will look at “screen” command which is actually a virtual terminal manager.
How to use “screen” command
Screen command is available for all UNIX based operating systems(OS X and Linux). This tool basically attaches a virtual invisible terminal to the processes and only if you quit the virtual terminal then only the process gets killed. We can insert input into virtual terminal and also see the output of virtual terminal in a real terminal. Creating a connection between virtual terminal and real terminal for input and output is called a session.
First connect to the remote host using SSH(or any other method) and then run the “screen” command(if not found then install it on the remote host). You would see something like this:
Now press enter key to start working with “screen” command. Now you will see a “screen” shell which has some characters difference from the terminal default shell(bash).
Now we would use the shell as we would. But whenever we need to attach a virtual terminal to a process, detach a session, list virtual terminals etc we would use Ctrl + A + [action_specific_key]
Attaching a Virtual Terminal to a Process
To launch a process with a virtual terminal first launch the process in the terminal running screen bash and while process is running press then key combination Ctrl + A + C. Suppose you are running ping command:
While its running press the key combination and you will end up creating a new virtual invisible terminal for the ping process and screen will bring you back to the real terminal running screen bash. So after pressing the key combination you would see something like this
Creating a session
To create a session(attach virtual terminal with real terminal) again use the key combinations Ctrl + A + P and Ctrl + A + N to create sessions between virtual terminal process. Ctrl + A + P allows you to create session with previous virtual terminal similarly Ctrl + A + N allows you to create session with next virtual terminal.
Drop a session
To drop a session(getting back to the screen command bash) use the key combination Ctrl + A + D.
Creating a Session when connection drops
Suppose your network connection dropped or you quit SSH and then logged in back, in that case run “screen” command with “-d -r” argument. You will get a list of process running virtual terminals. Create a session with anyone of them again by passing a third argument as the virtual terminal id
Getting back to SSH from Screen bash
To get back to SHH(or default terminal bash) you need to use the exit command.
Killing a Virtual Terminal Process
To kill a virtual terminal process you would first create a session with it and then kill it as its a normal processes using Ctrl + C.
If you want to kill it outside screen bash then list all the virtual terminal processes. And then run the command