Running 'Screen' On A Server By Default

Ever startet a long running process on a remote server when logged in via ssh? Ever lost the SSH connection while this process is runnig? Ever lost data because of this? Then ‘screen’ might be something for you.

I sure had this experience. Usually it’s a system upgrade which takes a considerable amount of time. When you google for this problem you are told “to better get used to to always type screen -R first thing when you log onto a remote server”. Screen spawns a kind of virtual shell. It is running in it’s own process not depending on your ssh connection. So if you loose your connection the shell keeps running on the server. You can reconnect an then reattach to the still running screen process.

This is very useful but I do not like the “better get used to type” part of this. So after a bit of research I came up with a solution to automate this. On remote servers I put the following line in my .bashrc-file

if $(screen -ls | grep -q pts); then  screen -x; else screen -R; fi

Now whenever I log in to this server I end up in a new screen session or in the one that is already running. And I can return to it any time after I lost my connection.

Written on January 27, 2017