我试图追踪终端中写入日志文件的所有内容。
在 1 号航站楼我这样做:
tail --retry --follow=name /tmp/test_log
或者:
tail -F /tmp/test_log
在终端 2 中我生成了以下测试输入tail
:
for N in $(seq 1 1000); do echo "$N" > /tmp/test_log; sleep 1; done
我不希望在这个测试用例中丢失信息,但我仍然得到以下输出,缺少数字 20 和 24:
tail: /temp/itworks: file truncated 18 tail: /temp/itworks: file truncated 19 tail: /temp/itworks: file truncated 21 tail: /temp/itworks: file truncated 22 tail: /temp/itworks: file truncated 23 tail: /temp/itworks: file truncated 25 tail: /temp/itworks: file truncated 26 tail: /temp/itworks: file truncated 27 ^C
但是,如果在终端 2 中重新写入之前删除日志,例如:
for N in $(seq 1 1000);
do
rm /tmp/test_log;
echo "$N" > /tmp/test_log;
sleep 1;
done
我得到了以下输出,这是人们所期望的替代方案,并且在长期运行中不会丢失任何信息:
1 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory 2 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory 3 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory 4 tail: ‘/tmp/test_log’ has appeared; following end of new file 5 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory tail: ‘/tmp/test_log’ has appeared; following end of new file 6 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory 7 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory 8 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory 9 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory tail: ‘/tmp/test_log’ has appeared; following end of new file 10 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory tail: ‘/tmp/test_log’ has appeared; following end of new file 11 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory 12 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory 13 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory 14 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory tail: ‘/tmp/test_log’ has appeared; following end of new file 15 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory tail: ‘/tmp/test_log’ has appeared; following end of new file 16 tail: ‘/tmp/test_log’ has become inaccessible: No such file or directory 17 ^C
由于我无法确定系统日志在被重写之前是否被删除:我该如何确保
echo "$N" > /tmp_test_log
版本能用吗?