lienahu
lienahu

Reputation: 157

keep program in ssh running when network lost

I have a problem how to keep my program run in ssh when the laptop(Mac) loses wifi-connection or network. I was running a python program remotely by ssh into a server, and before I run the code I made a new screen by entering 'screen'. And then I ran the program and pressed ctrl+A+D to detach the screen. Everything looked fine and the program continued to work when the laptop was closed( in a place with WIFI). However when I walked outside with my laptop for several minutes and I reopened the laptop it showed 'write fail: broken pipe' and the program stopped. I guess the problem happened because the laptop lost the network connection. Is there any way to fix this problem such that I could bring my laptop anywhere and keep my program run?

Upvotes: 5

Views: 3999

Answers (1)

fejese
fejese

Reputation: 4628

Open the screen on the remote server after SSHing in so you have a persistent session there, not on your local box.

If you did that, note that you'll still get disconnected if you loose connection but then SSHing in again and re-open the screen session to get back to work.

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

edit:

With using screen you get a permanent session that you can restore. This sessions will live where you start it. If you want to make sure that you keep running something on the remote server, then first SSH and then start the screen on the remote.

If you loose connection, then only your SSH connection will be terminated and you'll be disconnected from your screen session but that won't stop. You can SSH in again and reconnect to the screen session.

Try this:

local$ ssh remote.server
remote$ screen -S date
# screen starts with name 'date'. if it's 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 re-connecting to the screen session you should see the dates still going without any pause.

Upvotes: 7

Related Questions