我可以在Linux上监视文件重命名事件吗? [英] Can i monitor the file re-name event on linux?

查看:59
本文介绍了我可以在Linux上监视文件重命名事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,我使用节点将一些服务器消息记录到本地文件中,但有时文件太大,因此我将通过logroate工具(例如:将linz.log移至linz-2017-06-19.log,现在linz.log文件为空,但有时节点仍会将数据写入linz-2017-06-19.log,我想也许linz.log描述符未更改)

Usually i use node to log some server message into the local file, but sometimes the file too large, so i will regular archiving of this large file by logroate tool(etc: move the linz.log to the linz-2017-06-19.log, now the linz.log file is empty, but sometimes node will still write data into the linz-2017-06-19.log, i guess, maybe the linz.log descriptor not be changed)

推荐答案

您可以使用 inotifywait

inotifywait使用Linux的inotify(7)接口有效地等待文件更改.它适合等待shell脚本对文件的更改.它既可以在事件发生后退出,也可以在事件发生时不断执行并输出事件.

inotifywait efficiently waits for changes to files using Linux's inotify(7) interface. It is suitable for waiting for changes to files from shell scripts. It can either exit once an event occurs, or continually execute and output events as they occur.

示例:

touch /tmp/foofile
inotifywait -e move /tmp/
mv /tmp/foofile /tmp/barfile

inotifywait的输出

Output from inotifywait

Setting up watches.
Watches established.
/tmp/ MOVED_FROM foofile

这篇关于我可以在Linux上监视文件重命名事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆