我通常会在一个目录中观察许多日志做tail -f directory/*
. 问题是之后创建了一个新日志,它不会显示在屏幕上(因为*
已经展开了)。
有没有办法监视目录中的每个文件,即使是在进程启动后创建的文件?
Gil*_*il' 57
您可以使用... multitail来拖尾 多个文件。
multitail -Q 1 'directory/*'
Run Code Online (Sandbox Code Playgroud)
-Q 1 PATTERN
表示每 1 秒检查一次与PATTERN匹配的现有文件或新文件中的新内容。来自所有文件的行显示在同一窗口中,使用-q
而不是-Q
具有单独的窗口。
小智 9
您也可以使用以下方式观看目录watch
watch -n0,1 "ls -lrt /directory/ | tail"
Run Code Online (Sandbox Code Playgroud)
不知道 shell 解决方案,但是(假设 Linux 1
)inotify
可能是要走的路......请参阅这个模仿tail -F
(使用pyinotify
)的示例,也许它可以用作跟踪整个目录的基础。
一般情况下,inotify
可以监控目录(引用man 7 inotify
)
调用 inotify_add_watch(2) 时可以在掩码中指定以下位,并且可以在 read(2) 返回的掩码字段中返回以下位:
Run Code Online (Sandbox Code Playgroud)IN_ACCESS File was accessed (read) (*). IN_ATTRIB Metadata changed, e.g., permissions, timestamps, extended attributes, link count (since Linux 2.6.25), UID, GID, etc. (*). IN_CLOSE_WRITE File opened for writing was closed (*). IN_CLOSE_NOWRITE File not opened for writing was closed (*). IN_CREATE File/directory created in watched directory (*). IN_DELETE File/directory deleted from watched directory (*). IN_DELETE_SELF Watched file/directory was itself deleted. IN_MODIFY File was modified (*). IN_MOVE_SELF Watched file/directory was itself moved. IN_MOVED_FROM File moved out of watched directory (*). IN_MOVED_TO File moved into watched directory (*). IN_OPEN File was opened (*).
当监视的目录中,标有上述可发生在目录中的文件的星号(*),这些事件在该情况下,在目录中的文件的返回inotify_event结构标识的名称的名称字段。
(...并pyinotify
密切关注这些选项)
1
: BSD 也有类似的东西,kqueue
. 也许一个跨平台解决方案是可以实现使用GIO(Python绑定)作为抽象层,因为它可以,旁边inotify
,也可以使用kqueue