Skip to content

Instantly share code, notes, and snippets.

@wrouesnel
Created March 4, 2022 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrouesnel/a992fa19fb4e62f5432c82476b2e8b0e to your computer and use it in GitHub Desktop.
Save wrouesnel/a992fa19fb4e62f5432c82476b2e8b0e to your computer and use it in GitHub Desktop.
launch a properly passed through shell command in python
cmd = []
env = os.environ.copy()
p = subprocess.Popen(
cmd, env=env, cwd=os.path.realpath(os.curdir), stdin=sys.stdin, stderr=sys.stderr, stdout=sys.stdout
)
# Attach all signals and forward them to the subprocess
def sighandler(signum, stack):
p.send_signal(signum)
for i in [x for x in dir(signal) if x.startswith("SIG")]:
try:
signum = getattr(signal, i)
signal.signal(signum, sighandler)
except (OSError, RuntimeError, ValueError) as m: # OSError for Python3, RuntimeError for 2
#print("Skipping {}".format(i))
pass
p.wait()
sys.exit(p.returncode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment