( pid: number, exitCodePath: string, quotePath: (p: string) => string = shellQuote )
| 127 | * @param quotePath - Function to quote path (default: shellQuote). Use expandTildeForSSH for SSH. |
| 128 | */ |
| 129 | export function buildTerminateCommand( |
| 130 | pid: number, |
| 131 | exitCodePath: string, |
| 132 | quotePath: (p: string) => string = shellQuote |
| 133 | ): string { |
| 134 | const negPid = -pid; // Negative PID targets process group (PID === PGID due to set -m) |
| 135 | // Send SIGTERM, wait for process to exit, then write the correct exit code. |
| 136 | // We can't write immediately because the process's EXIT trap would overwrite it. |
| 137 | // After sleep 2, either the process exited (write SIGTERM code) or we escalate to SIGKILL. |
| 138 | return ( |
| 139 | `kill -15 ${negPid} 2>/dev/null || true; ` + |
| 140 | `sleep 2; ` + |
| 141 | `if kill -0 ${negPid} 2>/dev/null; then ` + |
| 142 | `kill -9 ${negPid} 2>/dev/null || true; ` + |
| 143 | `echo ${EXIT_CODE_SIGKILL} > ${quotePath(exitCodePath)}; ` + |
| 144 | `else ` + |
| 145 | `echo ${EXIT_CODE_SIGTERM} > ${quotePath(exitCodePath)}; ` + |
| 146 | `fi` |
| 147 | ); |
| 148 | } |
no outgoing calls
no test coverage detected