title: Kill process after a given time bash?
body: <p>I have a script that tries to make a DB connection using another program and the timeout(2.5min) of the program is to long. I want to add this functionality to the script.</p>
  
  <p>If it takes longer then 5 seconds to connect, kill the process<br>
  Else kill the sleep/kill process.</p>
  
  <p>The issue I'm having is how bash reports when a process is killed, that's because the processes are in the same shell just the background. Is there a better way to do this or how can I silence the shell for the kill commands?</p>
  
  <pre><code>DB_CONNECTION_PROGRAM &gt; $CONNECTFILE &amp;
  pid=$! 
  (sleep 5; kill $pid) &amp;
  sleep_pid=$!
  wait $pid
  
  # If the DB failed to connect after 5 seconds and was killed
  status=$? #Kill returns 128+n (fatal error)
  if [ $status -gt 128 ]; then
      no_connection="ERROR: Timeout while trying to connect to $dbserver"
  else # If it connected kill the sleep and any errors collect
      kill $sleep_pid
      no_connection=`sed -n '/^ERROR:/,$p' $CONNECTFILE`
  fi
  </code></pre>