5
基于inotify的Linux文件实时监控扫描
source link: https://misakikata.github.io/2020/09/%E5%9F%BA%E4%BA%8Einotify%E7%9A%84Linux%E6%96%87%E4%BB%B6%E5%AE%9E%E6%97%B6%E7%9B%91%E6%8E%A7%E6%89%AB%E6%8F%8F/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
watchfile
项目地址:https://github.com/MisakiKata/watchfile
#第三方库
pip install pyinotify
#使用supervisor来守护进程
apt-get install supervisor
yum install supervisor
#安装完成后需要启动,按需要修改配置文件
supervisord -c /etc/supervisor/supervisord.conf
#配置文件例子
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /data/vscode/watchfile/watchfile.conf #修改自己的配置文件地址
由于调用了河马扫描程序来检测文件,所以大量文件同时变更时,检测会慢,建议系统做大量变更时可以停掉程序。
在config.ini 中修改自己的配置,因为使用多线程来监控多目录,建议使用多目录配置
运行 python3 daemon.py来查看输出和测试邮件
异常处理
查看输出日志的时候,如果出现wd=-1
需要修改 vim /etc/sysctl.conf
fs.inotify.max_user_watches = 128000 #决定了同时同一用户可以监控的目录数量
日志中看到Event Queue Overflow
max_queued_events太小需要调整参数
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK