Currently removing, moving or renaming a file that has tail -f
running on it does nothing, and I'd like it to abort. I've read the man pages and it seems that -f should abort on file move and that -F will follow the file but on Mac OS X it seems -f and -F are the same. How can I write a bash script that makes tail -f exit cleanly after the file has been moved?
tail --follow=name
(rather than just -f
, which is equivalent to --follow=descriptor
) to achieve what you want, but ONLY if the file is DELETED rather than moved - once the file is deleted, an error message is reported and tail
exits (with code 1); sadly, by contrast, if the file is merely MOVED (renamed), tail
does NOT exit - necessitating a programmatic solution.bash script for exiting tailing once the target file no longer exists (under its original name) - more robust formulations of the script from @schellsan's own answer:
#!/usr/bin/env bash
tail -f "$1" & # start tailing in the background
while [[ -f $1 ]]; do sleep 0.1; done # periodically check if target still exists
kill $! 2>/dev/null || : # kill tailing process, ignoring errors if already dead
If more robustness is desired, here's a version that:
#!/usr/bin/env bash
# Set an exit trap to ensure that the tailing process
# - to be created below - is terminated,
# no matter how this script exits.
trap '[[ -n $tailPid ]] && kill $tailPid 2>/dev/null' EXIT
# Start the tailing process in the background and
# record its PID.
tail -f "$1" & tailPid=$!
# Stay alive as long as the target file exists.
while [[ -f $1 ]]; do
# Sleep a little.
sleep 0.1
# Exit if the tailing process died unexpectedly.
kill -0 $tailPid 2>/dev/null || { tailPid=; exit; }
done
Just in case anyone else runs into this problem you can use a small script in which you run tail as a background process and then loop until the file is moved, killing the tail process.
#!/bin/bash
tail -f $1 &
pid=$!
while [ -f $1 ]
do
if [ ! -f $1 ]
then
kill -9 $pid
fi
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With