Skip to content

Instantly share code, notes, and snippets.

@MatrixManAtYrService
Last active December 20, 2022 17:53
Show Gist options
  • Save MatrixManAtYrService/dbf4ef3b3ecca160214f3bf94afb92f4 to your computer and use it in GitHub Desktop.
Save MatrixManAtYrService/dbf4ef3b3ecca160214f3bf94afb92f4 to your computer and use it in GitHub Desktop.
capturing stdin, stdout, stderr, argv, pid, and return code, while forwarding (some) signals to a subprocess
[[ $SHIMAGE_PROCIO_VERBOSE -eq 1 ]] && set -x
# a folder for this process's outputs
DATA="{data}/$$"
mkdir -p $DATA
# capture argv
echo "$@" > $DATA/argv
# assume we'll be terminatged
# if we aren't, this will be overwritten
CODE=130
echo "$CODE" > $DATA/code
touch $DATA/stdin
touch $DATA/stdout
touch $DATA/stderr
function forward_sigusr1()
{
if [[ -f $DATA/pid ]]
then
PID=$(cat $DATA/pid)
kill -SIGUSR1 $PID 2> /dev/null
fi
}
function forward_sigusr2()
{
if [[ -f $DATA/pid ]]
then
PID=$(cat $DATA/pid)
kill -SIGUSR2 $PID 2> /dev/null
fi
}
function complete()
{
# send sigterm to child
PID=$(cat $DATA/pid)
kill -2 $PID 2> /dev/null
# enter venv
set +x
source /opt/shimage/bin/activate
[[ $SHIMAGE_PROCIO_VERBOSE -eq 1 ]] && set -x
# offload collected data
# presumably this is an ephemeral filesystem which k8s will be cleaning up soon
# 'shimage' is my tool for offloading the data to redis, where it can then be
# subscribed to remotely. I'll publish it on github once I can confince my company
# to open source it.
set -e
shimage procio offload \\
--stdin $DATA/stdin \\
--stdout $DATA/stdout \\
--stderr $DATA/stderr \\
--argv $DATA/argv \\
--pid $DATA/pid \\
--code $DATA/code &>2
# exit venv
set +x
deactivate
[[ $SHIMAGE_PROCIO_VERBOSE -eq 1 ]] && set -x
# wait for child process and retun its exit code to the caller
if [[ ! -z $CODE ]]
then
exit $CODE
else
wait $PID
exit $?
fi
}
# call the above functions depending on which signals we get
trap complete SIGTERM
# todo: test these
trap forward_sigusr1 SIGUSR1
trap forward_sigusr1 SIGUSR2
# capture stdin while teeing to the application
# capture stdout and stderr while teeing to the caller
# run the original program with the caller's args
set +e
cat - | tee $DATA/stdin | THE_COMMAND $@ 1> >(tee $DATA/stdout) 2> >(tee $DATA/stderr >&2) &
# grab its PID and wait for it to exit
jobs -p > $DATA/pid
wait $(cat $DATA/pid)
# capture exit code
echo $? > $DATA/code
CODE="$(cat $DATA/code)"
# offload data and exit with the above code
complete
@MatrixManAtYrService
Copy link
Author

This is part of a tool which is not ready for publication. Still I think it has some interesting ideas in it, so I wanted to share them ahead of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment