I would like to have a function in bash, that starts a program in the background, determines the PID of that program and also pipe's its output to sed
. I know how to do either one of them separately, but not how to achieve all of them at once.
What I have so far is this:
# Start a program in the background
#
# Arguments:
# 1 - Variable in which to "write" the PID
# 2 - App to execute
# 3 - Arguments to app
#
function start_program_in_background() {
RC=$1; shift
# Start program in background and determine PID
BIN=$1; shift
( $BIN $@ & echo $! >&3 ) 3>PID | stdbuf -o0 sed -e 's/a/b/' &
# ALTERNATIVE $BIN $@ > >( sed .. ) &
# Write PID to variable given as argument 1
PID=$(<PID)
# when using ALTERNATIVEPID=$!
eval "$RC=$PID"
echo "$BIN ---PID---> $PID"
}
The way I extract the PID is inspired by [1]. There is a second variant in the comments. Both of them show the output of the background processes when executing a script that starts programs using above function, but there is no output when I pipe
[1] How to get the PID of a process that is piped to another process in Bash?
Any ideas?