Save program to ssh when network is lost

I have a problem with how my program starts up in ssh when my laptop (Mac) loses wifi connection or network. I was running a python program remotely using ssh to the server, and before running the code, I made a new screen by typing "screen". Then I launched the program and pressed ctrl + A + D to detach the screen. Everything looked great and the program continued to work when the laptop was closed (in a place with WIFI). However, when I went outside with my laptop for a few minutes and I opened the laptop again, it showed "write fail: broken pipe" and the program stopped. I think the problem was caused by the laptop losing its network connection. Is there a way to fix this problem so that I can bring my laptop anywhere and save my program?

+3


source to share


1 answer


Open screen

on the remote server after SSHing so you have a persistent session and not in your local box.

If you did, note that you will disconnect anyway if you lose your connection, but then re-enable SSHing and start your session again to screen

get back to work.

local$ ssh remote.server
remote$ screen -ls # list screens
remote$ screen -dr <screen name> # force reconnect to screen session

      

change

With, screen

you get a persistent session that you can restore. These sessions will live where you start. If you want to make sure you run something on the remote server first SSH

and then run screen

on the remote.



If you lose your connection, then only your connection SSH

will be terminated and you will be disconnected from the session screen

, but this will not stop. You can SSH

connect to the session over and over again screen

.

Try the following:

local$ ssh remote.server
remote$ screen -S date
# screen starts with name 'date'. if it the first time you start screen on
# this box it might display some welcome message where you need to press enter

remote-screen$ while true; do date; sleep 1; done
# this will show the time every second

# disconnect your network: the ssh connection will be terminated
# open console again and continue

local$ ssh remote.server
remote$ screen -dr date

      

After reconnecting to the session, screen

you should see the dates continue without pauses.

+3


source







All Articles