Since I work mostly from home, I use ssh a lot.
The "broken pipe" disrupted my workflow continuously, esp.when some task running for a couple of minutes was killed.
At first, I fixed it in a rather n00bish way, by running the task in the background
('&' or (ctrl+z and bg)), and keeping ssh alive with a never ending while loop:
while true; do sleep 60; qstat -u ries; doneBut according to
https://askubuntu.com/questions/127369/how-to-prevent-write-failed-broken-pipe-on-ssh-connection
there are of course better ways:
send a keepalive message to the server
This is basically the same idea as with the while loop. Every 60 sec or so, a message is send to the server, telling it to keep your ssh connection stable.edit your
~/.ssh/ssh_config
with:Host * ServerAliveInterval 60
use the screen shell for long-running tasks
For tasks running longer than a couple of minutes (in my case it can be days), it doesn't really make much sense to keep a stable ssh connection, and your hoe computer up and running.
A better solution is to start the task in a way, that you can detach it for now, and get back to it later, while it goes on on it's own. Probably the best way to deal with this is screen, although it takes some time to get used to it.
Screen is a powerful utility that allows you to control multiple terminals which will stay alive independently of the ssh session.
While logged in via ssh, start screen
screen
you are now in a new screen shell. Start your command. For me s.th. like this:
python ../bwa_parallel_arrays/bwa_parallel.py -P tmp -I . -t 4 -e error -o out -n 10 > screen_out.txt &
detach the screen shell and get back to your previous terminal:
screen -d
Detach the screen session (disconnect it from the terminal and put it into the background). A detached screen can be resumed by invoking screen with the -r option.
screen and your task keep running on the host you logged on, as can be seen with the top command:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
55557 ries 20 0 3948712 3.686g 6392 R 5.9 1.5 0:41.99 python
32063 ries 20 0 27036 3524 2284 R 1.3 0.0 0:00.23 top
9023 ries 20 0 28976 3076 2440 S 0.0 0.0 0:00.00 screen
reattach your screen session with
screen -r
You can also check the status of your (various) screen sessions with
screen -list
There is a screen on:
9023.pts-8.waldorf (Detached)
1 Socket in /var/run/screen/S-ries.
using 'disown'
Out of convenience, I tend to just start the tasks in the background with '&', and detaching it from the shell with 'disown', so it doesn't get killed when the shell gets killed.
This is very easy, and can be done even after starting the task, by sending it to sleep background with Ctrl + Z, then running in the background with 'bg' and detaching it with 'disown'. This somehow doesn't work well with ssh. When the pipe breaks, the task breaks.
using 'nohup' [update]
Better than using 'disown' is to use 'nohup' (no hangup).Just prepend it to your command, and it will not terminate if the terminal gets closed.
Keine Kommentare:
Kommentar veröffentlichen