title: Python Subprocess Catch Process Error If There Is Any and Kill After 5 Seconds
body: <p>I would like to create subprocess which runs a node.js program (node app.js), catch if there is any process error and kill the process after 5 seconds whether there is any process error or not.</p>
  <p>First, I tried check_call method:</p>
  <pre><code>try:
      server = subprocess.check_call(&quot;node app.js&quot;)
      time.sleep(5)
      server.kill()
      server.terminate()
  except subprocess.CalledProcessError as e:
      print(e)
  </code></pre>
  <p>But problem is that when I start a subprocess that runs a node.js program with check_call method, if there is no process error, there is no way to kill the process except keyboard interrupt.</p>
  <p>I also tried subprocess.Popen:</p>
  <pre><code>try:
      server = subprocess.Popen(&quot;node app.js&quot; , stdout=subprocess.PIPE) 
      time.sleep(5)
      server.kill()
      server.terminate()
  except subprocess.CalledProcessError as e:
      print(e)
  </code></pre>
  <p>This time, I was able to kill the process after 5 seconds, but I could not catch the process error. When I tried it with server.communicate() I faced same problem as with check_call.</p>
  <p>What should I do to both catch a process error if there is any and kill subprocess after 5 seconds?</p>