Alex Biddle     About     Archive     Feed

Reconnect to a process after a broken pipe

Today, I needed to monitor the output of a long shell process over SSH. Unforunately there was a blip on the network and I got a broken pipe error. To cut a long story short, I decided it was easier to kill and restart the process. This, however, left the question: what if it happened again?

That’s when I remembered Screen, which I hadn’t used for a long time, being rather spoilt with tabbed terminals. Screen allows you to make sure that you don’t miss any console output even if SSH drops out. The idea is that Screen allows you to “detach” from a process, providing you with an ID that you can reattach to.

First start screen:

screen

This dumps you at a normal shell prompt. Start your long-running process then detach the terminal with:

./your_long_process < foo 
CTRL-a d

When you want to reattach to the process, get the screen ID and use the -r flag:

screen -ls
screen -r YOUR_ID_HERE

Later on in the evening, I could reattach my screen session from a laptop tethered to a mobile phone, SSH’d via a proxy to a computer on a LAN, knowing that I would not miss any console output if the connection dropped out again. The latency, on the other hand, was something quite special.