Prevent Write failed: Broken pipe problem during ssh session with screen command
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 :
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
Tutorials
+13.9k Golang : syscall.Socket example
+11.7k Golang : Sort and reverse sort a slice of runes
+6.7k Golang : Normalize email to prevent multiple signups example
+22.2k Golang : Convert Unix timestamp to UTC timestamp
+13.2k Facebook PHP getUser() returns 0
+5.5k Golang : Struct field tags and what is their purpose?
+22.6k Golang : Test file read write permission example
+18.8k Golang : Clearing slice
+4.7k Unix/Linux : secure copying between servers with SCP command examples
+5.2k How to check with curl if my website or the asset is gzipped ?
+8.5k Android Studio : Image button and button example
+9.2k Golang : Changing a RGBA image number of channels with OpenCV