The tail –pid=PID with -f option, will terminate the display once the process ID (PID) dies.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
## Create a script cat <<'EOF'> myscript.sh #!/bin/bash i=1 > myfile while [ $i -le 30 ] do echo "$i" >> myfile sleep 1 ((i++)) done EOF ## execute the script in background and note the pid chmod +x myscript.sh ./myscript.sh & ## PID => [1] 4883 ## tail --pid=PID option (replace the PID from above) tail -f --pid=4883 myfile ## returns 30 lines and autometically exit ./myscript.sh & ## execute the script in background ## default tail -f myfile ## returns 30 lines but does not exit ## press control + c to exist rm my* |