Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Last active August 16, 2024 19:04
Show Gist options
  • Save ormaaj/7effecc3ec939866c8ab9564eac4b2d6 to your computer and use it in GitHub Desktop.
Save ormaaj/7effecc3ec939866c8ab9564eac4b2d6 to your computer and use it in GitHub Desktop.
test pipeline pid access through jobs -p
0: 0,4
1: 0,5
2: 0,6
3: 0,2
0: 0,4
1: 4,0,5
2: 5,4,0,6
3: 6,5,4,0,2
#!/usr/bin/env -S -u sh -- ${sh}
# Requires linux and ksh93 (u+m or v-) or bash 5.3+
set +m
if [[ -v KSH_VERSION ]]; then
command set -o posix 2>/dev/null
builtin pids
[[ -v .sh.pid ]] ||
function .sh.pid.get {
(( .sh.value = ${ pids -f "%(pid)d"; } ))
}
[[ -v .sh.pgid ]] ||
function .sh.pgid.get {
(( .sh.value = ${ pids -f "%(pgid)d"; } ))
}
nameref \
sh_file=.sh.file \
sh_pid=.sh.pid \
sh_pgid=.sh.pgid
[[ -o xtrace ]] && .sh.fun.set () { set -x; }
fi
if [[ -v BASH_VERSION ]]; then
shopt -s lastpipe extglob expand_aliases
alias \
'nameref=typeset -n' \
'integer=typeset -i'
fi
if [[ -v BASH_VERSION ]]; then
typeset -gx BASH_COMPAT=51
nameref \
sh_file=BASH_SOURCE \
sh_pid=BASHPID \
sh_pgid='_sh_pgid[${|typeset -g _sh_pgid=; REPLY=0;}]'
function pgid {
nameref ret=$1
integer p=BASHPID
ret=$(ps -wwp "$p" -o pgid:1=)
}
typeset -g _sh_pgid
nameref sh_pgid='_sh_pgid[${|pgid "_sh_pgid[0]"; REPLY=0;}]'
fi
function p {
nameref o=$1
shift
typeset j
j=${ jobs -p; } j=${j//$'\n'/,}
print -u "$o" -f '%s: %s%s%s\n' -- \
"$1" ${j:+"${j},"} ${sh_pgid:+"${sh_pgid},"} "${sh_pid}"
}
alias 'p=p fd'
function m {
if (( sh_pid != 1 )); then
typeset rl=${ type -p readlink; }
typeset -x sh
sh=${ "$rl" -sn "/proc/${sh_pid}/exe"; } || return
exec unshare -Upf --map-auto --mount-proc -- "$sh_file"
fi
integer -a fd
{
p 0 | p 1 | p 2 | { exec {fd}>&-; fd=1; p 3; }
} {fd}>&1 | sort -unt : -k 1,1
}
m
# vim: set ft=bash fenc=utf-8 ff=unix ts=4 sw=4 noet :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment