diff options
Diffstat (limited to '')
| -rw-r--r-- | taskipy/task_runner.py | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/taskipy/task_runner.py b/taskipy/task_runner.py index cce472d..4c126dc 100644 --- a/taskipy/task_runner.py +++ b/taskipy/task_runner.py @@ -1,9 +1,13 @@ +import sys + +import psutil # type: ignore +import signal import subprocess from pathlib import Path from typing import List, Union -from taskipy.task import Task from taskipy.pyproject import PyProject +from taskipy.task import Task class TaskRunner: @@ -17,11 +21,26 @@ class TaskRunner: for command in task.commands: if task.runner is not None: command = f'{task.runner} {command}' - process = subprocess.Popen( command, shell=True, cwd=Path(self.__pyproject_path).parent ) + def send_signal_to_task_process(signum: int, frame) -> None: # pylint: disable=unused-argument + # pylint: disable=W0640 + psutil_process_wrapper = psutil.Process(process.pid) + is_direct_subprocess_a_shell_process = sys.platform != 'darwin' # pylint: disable=C0103 + + if is_direct_subprocess_a_shell_process: + # A shell is created because of Popen(..., shell=True) on linux only + # We want here to kill shell's child + sub_processes_of_taskipy_shell = psutil_process_wrapper.children() + for child_process in sub_processes_of_taskipy_shell: + child_process.send_signal(signum) + else: + process.send_signal(signum) + + signal.signal(signal.SIGTERM, send_signal_to_task_process) + try: process.wait() except KeyboardInterrupt: |