Magic of screen
July 12, 2020
The Linux command screen deserves to be more popular than it is currently. Here is a brief tutorial
Introduction
If you need to ssh to a remote server and run some commands, you will know that a common problem with ssh is that while you are running your commands, if the network becomes flaky and you lose the connection, your session is lost. It could mean anything from a minor irritant to a major catastrophe depending on what commands you were trying to run on the remote server. There is a a better way though. The Linux command, screen allows you to run a shell on the remote server, which keep running even if you lose the network connection. It means your your commands on the remote server will be uneffected by your losing the connection. You can simply re-connect and look at the running command as if you never lost network connection.
Steps
Screen is usually pre-install on most Linux distributions. Otherwise first install it on the remote server using your Linux distribution's package manager.
If you need to ssh to a remote server and run some commands, you will know that a common problem with ssh is that while you are running your commands, if the network becomes flaky and you lose the connection, your session is lost. It could mean anything from a minor irritant to a major catastrophe depending on what commands you were trying to run on the remote server. There is a a better way though. The Linux command, screen allows you to run a shell on the remote server, which keep running even if you lose the network connection. It means your your commands on the remote server will be uneffected by your losing the connection. You can simply re-connect and look at the running command as if you never lost network connection.
Steps
Screen is usually pre-install on most Linux distributions. Otherwise first install it on the remote server using your Linux distribution's package manager.
server $ sudo apt install screen
Lets say, you want to ssh to the remote server and start a long running command. May be you want to zip all the log files which may take a few minutes. Usually, you ssh to the remote server and run the command. But then while the command is running, you are stuck to your computer - you can't shut it down and go home. If the ssh session gets killed, your command will be killed too. So, we will use screen to keep a permanent shell running on the remote server and issue our commands within the screen shell.
Connect to the remote server as usual. You will get a shell prompt.
Connect to the remote server as usual. You will get a shell prompt.
desktop $ ssh <user@remote-server>
Before you do anything else, run this command to start a new screen session:
server $ screen -R -D
You will get another shell prompt. Now execute whatever command you originally wanted to run on the remote server.
server $ gzip myapp-*.log # run your long running command
Now, lets say while the above command is running, your network connection fails. Then the shell inside the screen will keep running. The above command will keep running as if nothing happened. You can simply re-connect:
desktop $ ssh <user@remote-server> # ssh again to the remote server server $ screen -R -D # reconnect to the screen session
You will see the gzip command running here.
Once the you are done on the remote server, exit from the shell as usual using Control-D.
Once the you are done on the remote server, exit from the shell as usual using Control-D.
server $ <Press Control-D>
That will bring you to the original shell, Control-D again to disconnect from the remote server.
server $ <Press Control-D>
Long running commands
If you are running a long running command on the remote shell, you may want to disconnect from the remote server and let the command run on the remote server. Then connect back some time later to check on the status
server $ <Press Control-A d>
Now you are in the regular ssh shell. Press Control-D now to disconnect from the remote server
server $ <Press Control-D>
Again, whenever you want to check on the commands running on the server, connect to the screen session again.
If you are running a long running command on the remote shell, you may want to disconnect from the remote server and let the command run on the remote server. Then connect back some time later to check on the status
server $ <Press Control-A d>
Now you are in the regular ssh shell. Press Control-D now to disconnect from the remote server
server $ <Press Control-D>
Again, whenever you want to check on the commands running on the server, connect to the screen session again.
desktop $ ssh <user@remote-server> # ssh again to the remote server server $ screen -R -D # reconnect to the screen session
Customise your screen
Once you are familiar with screen, You can create a file ~/.screenrc on the remote server which allows you to configure your screen sessions. Here is mine:
escape ^zz hardstatus alwayslastline "%H]%{=b bR} %w %=" vbell off
The above configuration changes the Control-A prefix used by screen to Control-Z. The second line prints a nice status at the bottom of the screen as a reminder that you are within the screen shell and the third and final line turns off the visual bell which otherwise flickers the screen any time there is an error.
Other tips
There are a lot more to the screen utility. You can explore using man screen if you end up liking it. Here is just one more pointer. Screen allows you to run multiple shells within a single screen session. So, you can run multiple commands at the same time. Also, you can give each of your screen shells a friendly name to help you remember what you are running within it.
I hope you give it a try!
Other tips
There are a lot more to the screen utility. You can explore using man screen if you end up liking it. Here is just one more pointer. Screen allows you to run multiple shells within a single screen session. So, you can run multiple commands at the same time. Also, you can give each of your screen shells a friendly name to help you remember what you are running within it.
I hope you give it a try!