make tail 等待文件存在

Sri*_*mar 31 tail ubuntu

tail -f bar/somefile.log当 somefile.log 不存在时会立即失败。如何让 tail 无限期地等待创建该文件(稍后会发生)?

更新:使用-F,我看到:

tail: cannot open `bar/somefile.log' for reading: No such file or directory
tail: cannot watch parent directory of `bar/somefile.log': No such file or directory
Run Code Online (Sandbox Code Playgroud)

因为bar尚不存在(稍后将创建)。当bar创建somefile.log并被触摸时,tail 根本没有接受更改。

小智 39

你没有提到你需要它用于哪个操作系统,但tail在 linux 上有 --retry 和 --follow 选项可以做到这一点;

tail --retry --follow=name somefile.log
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,或者`tail -F somefile.log`。但是,由于其他原因,tail 失败了(我已经更新了我的问题) (7认同)

cYr*_*rus 30

这有效:

while ! tail -f bar/somefile.log ; do sleep 1 ; done
Run Code Online (Sandbox Code Playgroud)


psu*_*usi 8

首先创建文件:

touch somefile ; tail -f somefile
Run Code Online (Sandbox Code Playgroud)