Fritzip
Fritzip

Reputation: 1317

Continuing script after long command executed over SSH

My local computer is running a bash script that executes another script (locally) on a remote like so :

#!/bin/bash
# do stuff
ssh remote@remote "/home/remote/path/to/script.sh"
echo "Done"
# do other stuff

script.sh takes around 15 minutes to execute. Without loss of connection, script.sh is executed completely (until the very last line). Though, Done will never be echoed (nor will the other stuff be executed).

Notes :

Edit : script.sh used for testing :

#!/bin/bash
touch /tmp/start
echo "Start..." & sleep 900; touch /tmp/endofscript

Upvotes: 2

Views: 1698

Answers (2)

Fritzip
Fritzip

Reputation: 1317

Adding -o ServerAliveInterval=60 fixes the issue.

The ServerAliveInterval option prevents your router from thinking the SSH connection is idle by sending packets over the network between your device and the destination server every 60 seconds. (source)

In the case of a script that takes several minutes to execute and that has no output, this will keep the connection alive and avoid it from timing out and being left hanging.

Two options : 

  • ssh -o ServerAliveInterval=60 remote@remote "/home/remote/path/to/script.sh"
  • Adding the following lines to ~/.ssh/config of local computer (replace remote by the name of your remote or * to enable for any remote):
    Host remote
        ServerAliveInterval 60
    

For additional information :

Upvotes: 3

MonsieurMemons
MonsieurMemons

Reputation: 98

Have you tried setting set -xv in the scripts, or executing both the scripts with bash -xv script.shto get the details of the scripts execution?

Upvotes: 0

Related Questions