timeout

run a commmand with a time limit

timeout [option] duration command [ args]

Start command, and kill it if still running after [ duration.

duration [seconds[s] | minutesm | daysd

--preserve-status Always exit with the status from command
--foreground when not running from a shell prompt, command reads from TTY and gets TTY signals; children of command will not be timed out
-k
--kill-after=duration
also send a KILL signal if command is still running this long after the initial signal was sent
-s
--signal=signal
signal to be sent on timeout; signal may be a name like 'HUP' or a number.
Default TERM.
 1) SIGHUP    2) SIGINT   3) SIGQUIT  4) SIGILL
 5) SIGTRAP  6) SIGABRT  7) SIGEMT   8) SIGFPE
 9) SIGKILL 10) SIGBUS  11) SIGSEGV 12) SIGSYS
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGURG
17) SIGSTOP 18) SIGTSTP 19) SIGCONT 20) SIGCHLD
21) SIGTTIN 22) SIGTTOU 23) SIGIO   24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM   27) SIGPROF 28) SIGWINCH
29) SIGINFO 30) SIGUSR1 31) SIGUSR2 
-v
--verbose
diagnose to stderr signals sent upon timeout
--help  
--version  

duration is a fixed point number with an optional suffix: s for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days. 0 disables the associated timeout.

If the command times out, and --preserve-status is not set, exit status 124.
Otherwise, exit with the status of Command<.
The TERM kills any process that does not block or catch it.
If the process traps TERM use the KILL which cannot be caught, in which case the exit status is 128+9 rather than 124. Maybe best to use a nested timeoutTERM outer longer KILL


BUGS Some platforms don't currently support timeouts beyond the year 2038.
GNU coreutils online help:

Report translation bugs
SEE ALSO kill(1)
Full documentation
or available locally via: info '(coreutils) timeout invocation'

/usr/bin/timeout utility is installed by default in most distros which is part of coreutils rpm in Linux

Check if coreutils is installed on your server

# rpm -q coreutils
coreutils-8.22-24.el7.x86_64
We can use bash utility with timeout to test SSH connection by checking port 22 status.
If you are using a different port for 22 then you can replace it in the below syntax

Syntax:

# timeout  bash -c "/"
Here server2 is my target host, I will execute the command with a timeout value of 5s on port 22

[root@server1 ~]# timeout 5 bash -c "