Solve the timeout problem in the script [shell, perl, ssh]

Source: Internet
Author: User

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.

 

 
 
  1. #!/bin/sh 
  2.   
  3. timeout() 
  4.         waitfor=3 
  5.         command=$* 
  6.         $command & 
  7.         commandpid=$! 
  8.   
  9.         ( sleep $waitfor ; kill -9 $commandpid > /dev/null 2>&1 ) & 
  10.   
  11.         watchdog=$! 
  12.         sleeppid=$PPID 
  13.         wait $commandpid > /dev/null 2>&1  
  14.   
  15.         kill $sleeppid > /dev/null 2>&1 
  16.   
  17. test123() 
  18.  
  19.         ifconfig 
  20.         sleep 10 
  21.         ifconfig 
  22. #        curl htpp://www.facebook.com 
  23.   
  24. 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

 
 
  1. #! /Usr/bin/env perl
  2. Use POSIX qw (strftime WNOHANG );
  3.  
  4. # Check input
  5. My $ timeout = shift @ ARGV;
  6. My ($ secs) = $ timeout = ~ /-- Timeout = (\ d +) $ /;
  7. Unless ($ secs)
  8. {
  9. Print "Usage:./timeout -- timeout = [SECONDS] [COMMAND] \ n ";
  10. Exit-1;
  11. }
  12.  
  13. # Fork and exec
  14. My $ status = 0;
  15. $ SIG {CHLD} = sub {while (waitpid (-1, WNOHANG)> 0) {$ status =-1 unless $? = 0; exit $ status ;}};
  16. $0 = 'timeout hacked'. $ ARGV [0];
  17. Defined (my $ child = fork );
  18. If ($ child = 0)
  19. {
  20. My $ cmd = join '', @ ARGV;
  21. Exec ($ cmd );
  22. }
  23. $ SIG {TERM} = sub {kill TERM => $ child };
  24. $ SIG {INT} = sub {kill INT => $ child };
  25.  
  26.  
  27. # Kill when timeout
  28. Sleep $ secs;
  29. $ Status =-1;
  30. Kill TERM => $ child;
  31. Sleep 1 and kill INT => $ child if kill 0 => $ child;
  32. Sleep 1 and kill KILL => $ child if kill 0 => $ child;
  33. Exit $ status;
  34. # 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.