ssh

Remote script execution using ssh timeout after 10 minutes


I am trying to execute the remote shell script on a linux machine from another linux machine using SSH

It works perfectly fine for most of the cases, however I have a script which is has inactivity of more than 10 minutes, my connection to remote machine is lost.

Remote Shell Script

test.sh

echo "testing"
sleep 3600
echo "done testing"

And this is How I am calling this script

➜  ~ ssh -o ConnectTimeout=3600 -o BatchMode=yes -o StrictHostKeyChecking=no gaurang.shah@host_name /home/gaurang.shah/test.sh
*******************************************************************************
*                                                                             *
*          Unauthorized use of this system is strictly prohibited             *
*                                                                             *
*******************************************************************************

testing
Connection to host_name closed by remote host.

Shouldn't this parameter keep the connection alive for 3600 seconds ?

-o ConnectTimeout=3600


Solution

  • ConnectTimeout specifies the timeout (in seconds) used when connecting to the SSH server

    You have to set ServerAliveInterval instead. To set it for all users you have to modify /etc/ssh/ssh_config (having root grants). To enable just your user modify ~/.ssh/config

    Host *
        ServerAliveInterval 3600
        ServerAliveCountMax 2
    

    If you can modify the server side, you can make your server keeps alive all connections with clients by adding the following to /etc/ssh/sshd_config:

    ClientAliveInterval 3600
    ClientAliveCountMax 2