python - Terminate a gnome-terminal opened with subprocess -


using subprocess , command 'gnome-terminal -e bash' can open gnome-terminal desired (and have stick around). done either

p=subprocess.popen(['gnome-terminal', '-e', 'bash']) 

or

p=subprocess.popen(['gnome-terminal -e bash'], shell=true) 

but cannot close terminal using p.terminate() or p.kill(). understand, little trickier when using shell=true did not expect run problems otherwise.

to terminate terminal , children (in same process group):

#!/usr/bin/env python import os import signal import subprocess  p = subprocess.popen(['gnome-terminal', '--disable-factory', '-e', 'bash'],                      preexec_fn=os.setpgrp) # here... os.killpg(p.pid, signal.sigint) 
  • --disable-factory used avoid re-using active terminal can kill newly created terminal via subprocess handle
  • os.setpgrp puts gnome-terminal in own process group os.killpg() used send signal group

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -