Prevent Write failed: Broken pipe problem during ssh session with screen command



Tags : macosx terminal screen broken-pipe

There are times when you are away from you computer/terminal and come back later to find the ssh session or terminal window to another server become unresponsive and terminated with the error message :

Write failed: Broken pipe

Diagnostic :

Your terminal session may get closed due to various network issues. Such as your computer goes into sleep mode and cause the WiFI connection to drop. So you may want to increase the time before your computer go into sleep mode.

Solution :

Another solution will allow you to keep your ssh session even when the network connection is dropped is to use the screen command. The screen program is designed to allow programs started from the terminal to be separated from the original terminal shell and it also allows you to detach/attach terminal sessions. The only draw back is that you need to keep track of the screens PIDs on paper. However, if you are not running a lot of terminal sessions simultaneously you should be ok.

First : run

>screen -list

No Sockets found in /var/folders/nd/5m9x1vj4338yt1rg02zbph0000gn/T/.screen.

This means there are no screen attached.

Next :

>ssh root@remote-machine-hostname

to connect to your remote server. Run some command ... such as compiling a program source code or download huge file. Normally, if you encounter a "Broken pipe" error here... all your currently executing tasks that were invoked by the ssh terminal will fail and you will have to start all over again when reconnected.

List the screen again

>screen -list

 There is a screen on:
  3673.ttys000.Sweets-Mac-Pro (Attached)
 1 Socket in /var/folders/nd/5m9x1v_j4338y_t1rg02zbph0000gn/T/.screen.

as you can see, there is a screen attached to PID 3674. Next we will press Ctrl-A + D buttons, this will cause the terminal to fall back to your local machine and you will see :

[detached]

and if you do a screen -list again, the message will now be :

 There is a screen on:
  3673.ttys000.Sweets-Mac-Pro (Detached)
 1 Socket in /var/folders/nd/5m9x1v_j4338y_t1rg02zbph0000gn/T/.screen.

This is as good as having a network disconnection issue. However, the command that you executed on the remote is still running!

Finally :

Let say you manage to get the network disconnection issue fixed and want to reconnect back. Just execute

>screen -r

or

>screen -r 3673 if you know the PID.

and instantly you will reconnect back to the remote server without having to deal with unresponsive terminal and Write failed: Broken pipe error.

Cool! I'm starting to like this screen program!

Reference :

https://www.gnu.org/software/screen/manual/screen.html

Affiliate :

Hire Golang Developers




Tags : macosx terminal screen broken-pipe

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement