我可以使用 ls --sort=time |
但我正在寻找也能连续运行第一部分的东西,即如果在 tail 命令运行时创建了新文件,它会切换到尾随新文件。我考虑过使用 tail -f
最后修改的文件头-1 | xargs tail -f watch
命令,但这似乎与 tail -f
配合使用效果不佳
最佳答案
看来你需要这样的东西:
#!/bin/bash
TAILPID=0
WATCHFILE=""
trap 'kill $(jobs -p)' EXIT # Makes sure we clean our mess (background processes) on exit
while true
do
NEWFILE=`ls --sort=time | head -n 1`
if [ "$NEWFILE" != "$WATCHFILE" ]; then
echo "New file has been modified"
echo "Now watching: $NEWFILE";
WATCHFILE=$NEWFILE
if [ $TAILPID -ne 0 ]; then
# Kill old tail
kill $TAILPID
wait $! &> /dev/null # supress "Terminated" message
fi
tail -f $NEWFILE &
TAILPID=$! # Storing tail PID so we could kill it later
fi
sleep 1
done
关于linux - 尾部最后修改的文件并监视 bash 中的新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36197824/