Created
          August 28, 2018 14:14 
        
      - 
      
 - 
        
Save viveksyngh/b34c295984a9888d2badcc42f3d7d65c to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | func CommandTimeoutWithTimer(command string, timeout time.Duration) (string, error) { | |
| cmd := exec.Command("/bin/bash", "-c", command) | |
| timer := time.AfterFunc(timeout, func() { | |
| cmd.Process.Kill() | |
| }) | |
| out, err := cmd.CombinedOutput() | |
| isExpired := timer.Stop() | |
| if isExpired == false { | |
| fmt.Println("Command timed out") | |
| return "Command timed out", errors.New("Command Timeout") | |
| } | |
| if err != nil { | |
| log.Printf("Error : %s", err.Error()) | |
| return string(out), err | |
| } | |
| return string(out), nil | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment