brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tail can stop following a file once the writing process ends

Tail
~/tips/tip-tail-pid-stop-following

tail -f --pid=1234 build.log

Watch a job's output and get your prompt back when the job finishes.

Following a build log with -f never ends by itself, even after the build is long done. GNU tail can watch the process too.

tail -f --pid=1234 build.log

When process 1234 exits, tail prints what is left and quits. In a script, $! gives you the ID of the job you just started in the background:

./build.sh > build.log 2>&1 &
tail -n +1 -f --pid=$! build.log
echo "build finished"

The -n +1 shows the file from the first line, so nothing written before tail started is missed. The last line runs once the build is over.

For something that is already running, find the ID first:

pgrep -f build.sh

I like it in CI style scripts, where output should stream live and the script must continue afterwards. To get the exit status of the job itself, add wait $! after tail.

This is a GNU option, so check tail --help on other systems before relying on it. It watches only the one process. If that process starts a child that keeps writing and then exits, tail stops while output is still coming. Process IDs are also reused eventually, so do not store one and use it an hour later.

Further reading: tail invocation

More Tail tips tail -c 2000 export.json tail -f /var/log/nginx/access.log /var/log/nginx/error.log tail -n +2 export.csv tail -F /var/log/nginx/access.log All Tail tips (5) All topics

Link