--- HAUTZ Gilles <[EMAIL PROTECTED]> wrote:

> Andrew Benton a écrit :
> 
> > HAUTZ Gilles wrote:
> >
> >> Hi all,
> >>
> >> I've discovered a "bug" in the ntp boot script.
> >> If there is no network connectivity or a DNS problem. The computer 
> >> won't boot and stay at NTP starting.
> >
> > Maybe it would just be simpler to set a time limit of 30 
> > seconds or something and if it hasn't connected to the time server the 
> > script should fail with a warning?
> 
> I think the timeout is the best solution, but how to implement that with 
> the bootscript ?

Here is a generic method to implement a timeout in BASH. Suppose you have a
script that potentially takes a long time to execute. Such a script looks
like this:

------------------------------------------------------------------------     
  
#!/bin/bash

sleep 1000
------------------------------------------------------------------------     
  

In this case, "sleep 1000" represents the command or sequence of commands
that can potentially take too long. If you want to implement a timeout on the
whole script, make the following change:

------------------------------------------------------------------------     
  
#!/bin/bash                                                                  
  
                                                                             
  
ME="$(basename "$0")"                                                        
    
timeout=3                                                                    
  
export script_pid=$$                                                         
  
(                                                                            
  
    sleep $timeout                                                           
  
    if kill $script_pid &>/dev/null                                          
  
    then                                                                     
  
        echo "Time limit of $timeout seconds exceeded; terminating $ME."     

    fi                                                                       
  
) &
                                                                             
  
sleep 1000
------------------------------------------------------------------------     
  

The added paragraph launches a background subshell whose job is to wait for
$timeout seconds. After $timeout seconds have elapsed, it checks to see if
the script is still running, and if so, terminates it. 

You can add that paragraph to the beginning of any script and it should
guarantee that the script takes no longer than $timeout seconds to exit. If
the timeout is exceeded, the script will terminate with a nonzero exit
status. The precise exit status of a killed script is defined by the
operating system as 128 + SIGTERM = 143. SIGTERM has a value of 15, and is
the default signal that is sent by the kill command.


                
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com
-- 
http://linuxfromscratch.org/mailman/listinfo/blfs-support
FAQ: http://www.linuxfromscratch.org/blfs/faq.html
Unsubscribe: See the above information page

Reply via email to