Read logs from one process during run of some command

This is made rather straightforward by sending your background processes to, well, the background:

foo.sh &
mypid=$!
tail -f /path/to/logs.log > /path/to/partial.log &
tailpid=$!
wait $mypid
kill -TERM $tailpid

$! captures the PID of the last job sent to run in the background, so we can wait on your script to finish, and then kill the tail process when we no longer need it.


This version can do it too (i think):

( tail -f logs.log >foo_part.log &
foo.sh&
wait $! && kill %1 ) 

Note that %1 will hit the first background process in the subshell

Tags:

Bash

Tail