You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.2 KiB
Python

import asyncio, threading
# Maybe add: https://docs.python.org/3/library/shlex.html#shlex.quote ?
async def run_command_shell(command, grc=False):
"""Run command in subprocess (shell)."""
kill = lambda proc: proc.kill()
# Create subprocess
process = await asyncio.create_subprocess_shell(
command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
# Status
print("Started:", command, "(pid = " + str(process.pid) + ")", flush=True)
kill_timer = threading.Timer(60, kill, [process])
try:
# Wait for the subprocess to finish
kill_timer.start()
stdout, stderr = await process.communicate()
except:
kill_timer.cancel()
# Progress
if process.returncode == 0:
print("Done:", command, "(pid = " + str(process.pid) + ")", flush=True)
# Result
result = stdout.decode().strip()
else:
print("Failed:", command, "(pid = " + str(process.pid) + ")", flush=True)
# Result
result = stderr.decode().strip()
kill_timer.cancel()
if not grc:
# Return stdout
return result
else:
return process.returncode, result