Reputation: 7931
I'm executing a script connecting via password-less SSH on a remote host. I want to set a timeout, so that if the remote host is taking an infinite time to run, I want to come out of that ssh session and continue other lines in my sh script.
How can I set a timeout?
Upvotes: 295
Views: 452325
Reputation: 474
just adding the following .ssh/config
snippet that I use,
ServerAliveInterval 20
ServerAliveCountMax 5
ConnectTimeout 10
ConnectTimeout Specifies the timeout (in seconds) used when connecting to the SSH server, instead of using the default system TCP timeout. This timeout is applied both to establishing the connection and to performing the initial SSH protocol handshake and key exchange. (from the doc)
Upvotes: 6
Reputation: 7851
Well, you could use nohup to run whatever you are running on 'non-blocking mode'. So you can just keep checking if whatever it was supposed to run, ran, otherwise exit.
nohup ./my-script-that-may-take-long-to-finish.sh & ./check-if-previous-script-ran-or-exit.sh
echo "Script ended on Feb 15, 2011, 9:20AM" > /tmp/done.txt
So in the second one you just check if the file exists.
Upvotes: 0
Reputation: 7931
ssh -o ConnectTimeout=10 <hostName>
Where 10 is time in seconds. This Timeout applies only to the creation of the connection.
Upvotes: 469
Reputation: 419
If all else fails (including not having the timeout
command) the concept in this shell script will work:
#!/bin/bash
set -u
ssh $1 "sleep 10 ; uptime" > /tmp/outputfile 2>&1 & PIDssh=$!
Count=0
while test $Count -lt 5 && ps -p $PIDssh > /dev/null
do
echo -n .
sleep 1
Count=$((Count+1))
done
echo ""
if ps -p $PIDssh > /dev/null
then
echo "ssh still running, killing it"
kill -HUP $PIDssh
else
echo "Exited"
fi
Upvotes: 4
Reputation: 2732
You could also connect with flag
-o ServerAliveInterval=<secs>so the SSH client will send a null packet to the server each
<secs>
seconds, just to keep the connection alive.
In Linux this could be also set globally in /etc/ssh/ssh_config
or per-user in ~/.ssh/config
.
Upvotes: 31
Reputation: 3903
Use the -o ConnectTimeout
and -o BatchMode=yes -o StrictHostKeyChecking=no
.
ConnectTimeout keeps the script from hanging, BatchMode keeps it from hanging with Host unknown, YES to add to known_hosts, and StrictHostKeyChecking adds the fingerprint automatically.
**** NOTE **** The "StrictHostKeyChecking" was only intended for internal networks where you trust you hosts. Depending on the version of the SSH client, the "Are you sure you want to add your fingerprint" can cause the client to hang indefinitely (mainly old versions running on AIX). Most modern versions do not suffer from this issue. If you have to deal with fingerprints with multiple hosts, I recommend maintaining the known_hosts file with some sort of configuration management tool like puppet/ansible/chef/salt/etc.
Upvotes: 124
Reputation: 1267
try this:
timeout 5 ssh user@ip
timeout executes the ssh command (with args) and sends a SIGTERM if ssh doesn't return after 5 second. for more details about timeout, read this document: http://man7.org/linux/man-pages/man1/timeout.1.html
or you can use the param of ssh:
ssh -o ConnectTimeout=3 user@ip
Upvotes: 77