title: Kill a sudo/root process with pexpect
body: <p>Given a pexpect spawned process that's opened with sudo, like so:</p>
  
  <pre><code>#!/usr/bin/env python
  import pexpect
  
  cmd = ['sudo', 'bash', '-c', '"some long-running sudo command"']
  cmd = ' '.join(cmd)
  child = pexpect.spawn(cmd, timeout=60)
  
  i = child.expect([
    'success',
    'error'])
  if i == 0:
    print('ok')
  else:
    print('fail')
    # insert code here
  </code></pre>
  
  <p>How would I kill this process on fail (or success, for that matter)?</p>
  
  <p>I've tried the following (replacing <code># insert code here</code>):</p>
  
  <pre><code>child.kill(0)
  child.close(force=True)
  </code></pre>
  
  <p>Both give the following error, which makes sense as <em>the Python script is not a root process</em>, and it's trying to <em>kill something that is a root process</em>.</p>
  
  <pre><code>Traceback (most recent call last):
    File "./myscript.py", line 85, in &lt;module&gt;
      requires_qemu()
    File "./myscript.py", line 82, in requires_qemu
      child.close(0)
    File "/usr/lib/python2.7/site-packages/pexpect/__init__.py", line 747, in close
      raise ExceptionPexpect('Could not terminate the child.')
  pexpect.ExceptionPexpect: Could not terminate the child.
  </code></pre>
  
  <p>It is not possible to run the script as root, due to file permissions (run from a shared NFS drive where root access is blocked)</p>