foxshell/fn/-z4h-run-process-tree
2025-07-04 11:48:40 -05:00

42 lines
995 B
Bash

#!/usr/bin/env zsh
#
# Based on https://github.com/romkatv/run-process-tree but without
# the preservation of options, patterns, etc.
emulate -L zsh || return
setopt monitor traps_async pipe_fail no_unset no_bg_nice || return
zmodload zsh/system || return
local stdout REPLY
exec {stdout}>&1 || return
{
{
local -i pipe
local sig=(EXIT HUP ILL INT PIPE QUIT TERM ZERR)
local trap=(trap "trap - $sig; kill -- -$sysparams[pid]" $sig)
exec {pipe}>&1 1>&$stdout || return
$trap
{
$trap
while command sleep 1 && print -u $pipe .; do; done
} 2>/dev/null &
local -i watchdog=$!
{
trap - ZERR
exec {pipe}>&- || return
() { "$@" } "$@"
} &
local -i ret
wait $! || ret=$?
trap "exit $ret" TERM
kill $watchdog || return
wait $watchdog || return
return ret
} | while read; do; done || return
} always {
exec {stdout}>&-
}