Thursday, October 19, 2023

SCREEN Unix/Linux Command: An Oldie But A Goody!

 Back in the day, when listening to AM Radio to hear all the latest and greatest pop hits, every now and then, the DJ would announce that he would play "an oldie but a goody" - which was a song no longer on the charts, but still very popular.  Here are a few to listen to if you have the time: 

OLDIES_BUT_GOODIES

I recently ran into a spurious issue where normally stable terminal sessions started to detach for no known reason.  It is probably some kind of network instability.  We had the network timeout settings for our sessions set to zero (no maximum). See Keep Alive or Never time out

 Keep in mind that on some systems, security may limit this.  

So what do you do when you have a long running process and there is no way to keep it in the background?  For instance - the process runs for quite some time, but occasionally requires user input such as a password or application information.  

I recently ran into such a situation.  Trying to build a rather large database from a backup can take hours.  Building this database via a tooling script from a cloud provider required intermittent responses with passwords and wallet keys, but no way to input them as parameters in advance (poorly written, if you ask me).  

This brings us to using the SCREEN command.  It certainly serves the purpose, but it can be a little "kludgey" and may be different in each linux/unix variant.  In fact, it is not always present and may need to be installed:

sudo apt install screen

An example of it's use is hard to depict, since it involves a session gone awry, but step-by-step:

1) Create a (saved) screen session:

screen -S mysavedsession

2) Verify its presence:

$ screen -ls


There is a screen on:

61450.savethis (Detached)

1 Socket in /var/run/screen/S-opc.

3) Run a command with continuous output:

ping cnn.com

From 192.168.100.2 (192.168.100.2) icmp_seq=1 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=2 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=3 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=4 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=5 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=6 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=7 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=8 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=9 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=10 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=11 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=12 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=13 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=14 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=15 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=16 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=17 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=18 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=19 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=40 Packet filtered

4) terminate the session....(do not cntrl-c!) ...literally close the terminal window while it is running.

5) Start another session with the same user account: 

ssh opc@myserver

6) Again (optionally) verify existence of the session.  It should appear as "(Detached)" which is what we want!:

screen -ls

There is a screen on:

61450.savethis (Detached)

1 Socket in /var/run/screen/S-opc.


$ screen -r savethis. # output should resume:

From 192.168.100.2 (192.168.100.2) icmp_seq=30 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=31 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=32 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=33 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=34 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=35 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=36 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=37 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=38 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=39 Packet filtered

From 192.168.100.2 (192.168.100.2) icmp_seq=40 Packet filtered

This can be useful whether you have a long running job, or simply decide to get up for a cup of coffee.  You never know when your connection could be lost, either intentionally or due to an unforeseen event!