Sometimes we execute a script, because some programs in the script are suspended and time-out, which affects our judgment on the next step. At will, it is necessary to set the timeout settings for functions and processes so that they can return a state after a period of no response.
In the command parameters, ssh-o ServerAliveInterval = 60 can be controlled to 60 seconds.
For example, if ClientAliveInterval = 15 and ClientAliveCountMax = 3, it will be sent once every 15 seconds, once every 30 seconds, and once every 45 seconds. If all three attempts fail, the link will be recalled.
However, this parameter is not easy to use. You can add sleep 100 next to it.
- #!/bin/sh
-
- timeout()
- {
- waitfor=3
- command=$*
- $command &
- commandpid=$!
-
- ( sleep $waitfor ; kill -9 $commandpid > /dev/null 2>&1 ) &
-
- watchdog=$!
- sleeppid=$PPID
- wait $commandpid > /dev/null 2>&1
-
- kill $sleeppid > /dev/null 2>&1
- }
-
- test123()
- {
-
- ifconfig
- sleep 10
- ifconfig
- # curl htpp://www.facebook.com
- }
-
- timeout test123
650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131227/1T52a427-0.jpg "/>
Basically controlled to about 3 s
There is also a perl version
- #! /Usr/bin/env perl
- Use POSIX qw (strftime WNOHANG );
-
- # Check input
- My $ timeout = shift @ ARGV;
- My ($ secs) = $ timeout = ~ /-- Timeout = (\ d +) $ /;
- Unless ($ secs)
- {
- Print "Usage:./timeout -- timeout = [SECONDS] [COMMAND] \ n ";
- Exit-1;
- }
-
- # Fork and exec
- My $ status = 0;
- $ SIG {CHLD} = sub {while (waitpid (-1, WNOHANG)> 0) {$ status =-1 unless $? = 0; exit $ status ;}};
- $0 = 'timeout hacked'. $ ARGV [0];
- Defined (my $ child = fork );
- If ($ child = 0)
- {
- My $ cmd = join '', @ ARGV;
- Exec ($ cmd );
- }
- $ SIG {TERM} = sub {kill TERM => $ child };
- $ SIG {INT} = sub {kill INT => $ child };
-
-
- # Kill when timeout
- Sleep $ secs;
- $ Status =-1;
- Kill TERM => $ child;
- Sleep 1 and kill INT => $ child if kill 0 => $ child;
- Sleep 1 and kill KILL => $ child if kill 0 => $ child;
- Exit $ status;
- # Usage./t -- timeout = 3 curl http://www.facebook.com
The above shows that perl actually uses the same method ~
This article is from "Fengyun, it's her ." Blog, please be sure to keep this source http://rfyiamcool.blog.51cto.com/1030776/1189520