Bash: wait for process to exit while tailing log file

In a bash script, I'm waiting on a child process's pid using wait. That child process is writing to a log file. Is there a way in the bash script to tail that log file to std out while at the same time waiting on the process to complete?

Answers


Use the tail command to follow the file while you wait for the command to finish.

command &
cmdpid=$!
tail -f -n +0 logfile &
wait $cmdpid
kill $!

This is in spirit similar to William's solution, but with one important difference: it will correctly print the log file if it takes longer for command to finish than it does for cat to print the file (quite likely, as cat is very fast). The -n +0 option tells tail to print the whole file before it starts following updates.


Need Your Help

Screen capture during video preview fails

ios swift avfoundation

I'm trying to capture the screen while doing a video preview with AVFoundation (AVCaptureDeviceInput and AVCaptureVideoDataOutput)

Radio button post data of multiple input fields

php html forms post radio-button

I have a form in my PHP page which is created by a loop through an array.