Created
March 4, 2022 10:33
-
-
Save wrouesnel/a992fa19fb4e62f5432c82476b2e8b0e to your computer and use it in GitHub Desktop.
launch a properly passed through shell command in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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