mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-23 21:20:42 +08:00
commit
947157c370
@ -1,111 +0,0 @@
|
||||
translating---geekpi
|
||||
|
||||
Pyinotify – Monitor Filesystem Changes in Real-Time in Linux
|
||||
============================================================
|
||||
|
||||
Pyinotify is a simple yet useful Python module for [monitoring filesystems changes][1] in real-time in Linux.
|
||||
|
||||
As a System administrator, you can use it to monitor changes happening to a directory of interest such as web directory or application data storage directory and beyond.
|
||||
|
||||
**Suggested Read:** [fswatch – Monitors Files and Directory Changes or Modifications in Linux][2]
|
||||
|
||||
It depends on inotify (a Linux kernel feature incorporated in kernel 2.6.13), which is an event-driven notifier, its notifications are exported from kernel space to user space via three system calls.
|
||||
|
||||
The purpose of pyinotiy is to bind the three system calls, and support an implementation on top of them providing a common and abstract means to manipulate those functionalities.
|
||||
|
||||
In this article, we will show you how to install and use pyinotify in Linux to monitor filesystem changes or modifications in real-time.
|
||||
|
||||
#### Dependencies
|
||||
|
||||
In order to use pyinotify, your system must be running:
|
||||
|
||||
1. Linux kernel 2.6.13 or higher
|
||||
2. Python 2.4 or higher
|
||||
|
||||
### How to Install Pyinotify in Linux
|
||||
|
||||
First start by checking the kernel and Python versions installed on your system as follows:
|
||||
|
||||
```
|
||||
# uname -r
|
||||
# python -V
|
||||
```
|
||||
|
||||
Once dependencies are met, we will use pip to install pynotify. In most Linux distributions, Pip is already installed if you’re using Python 2 >=2.7.9 or Python 3 >=3.4 binaries downloaded from python.org, otherwise, install it as follows:
|
||||
|
||||
```
|
||||
# yum install python-pip [On CentOS based Distros]
|
||||
# apt-get install python-pip [On Debian based Distros]
|
||||
# dnf install python-pip [On Fedora 22+]
|
||||
```
|
||||
|
||||
Now, install pyinotify like so:
|
||||
|
||||
```
|
||||
# pip install pyinotify
|
||||
```
|
||||
|
||||
It will install available version from the default repository, if you are looking to have a latest stable version of pyinotify, consider cloning it’s git repository as shown.
|
||||
|
||||
```
|
||||
# git clone https://github.com/seb-m/pyinotify.git
|
||||
# cd pyinotify/
|
||||
# ls
|
||||
# python setup.py install
|
||||
```
|
||||
|
||||
### How to Use pyinotify in Linux
|
||||
|
||||
In the example below, I am monitoring any changes to the user tecmint’s home (/home/tecmint) directory as root user (logged in via ssh) as shown in the screenshot:
|
||||
|
||||
```
|
||||
# python -m pyinotify -v /home/tecmint
|
||||
```
|
||||
[
|
||||
![Monitor Directory Changes](http://www.tecmint.com/wp-content/uploads/2017/03/Monitor-Directory-File-Changes.png)
|
||||
][3]
|
||||
|
||||
Monitor Directory Changes
|
||||
|
||||
Next, we will keep a watch for any changes to the web directory (/var/www/html/tecmint.com):
|
||||
|
||||
```
|
||||
# python -m pyinotify -v /var/www/html/tecmint.com
|
||||
```
|
||||
|
||||
To exit the program, simply hit `[Ctrl+C]`.
|
||||
|
||||
Note: When you run pyinotify without specifying any directory to monitor, the `/tmp` directory is considered by default.
|
||||
|
||||
Find more about Pyinotify on Github: [https://github.com/seb-m/pyinotify][4]
|
||||
|
||||
That’s all for now! In this article, we showed you how to install and use pyinotify, a useful Python module for monitoring filesystems changes in Linux.
|
||||
|
||||
Have you come across any similar Python modules or related [Linux tools/utilities][5]? Let us know in the comments, perhaps you can as well ask any question in relation to this article.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
作者简介:
|
||||
|
||||
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/pyinotify-monitor-filesystem-directory-changes-in-linux/
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/aaronkili/
|
||||
|
||||
[1]:http://www.tecmint.com/fswatch-monitors-files-and-directory-changes-modifications-in-linux/
|
||||
[2]:http://www.tecmint.com/fswatch-monitors-files-and-directory-changes-modifications-in-linux/
|
||||
[3]:http://www.tecmint.com/wp-content/uploads/2017/03/Monitor-Directory-File-Changes.png
|
||||
[4]:https://github.com/seb-m/pyinotify
|
||||
[5]:http://tecmint.com/tag/commandline-tools
|
||||
[6]:http://www.tecmint.com/author/aaronkili/
|
||||
[7]:http://www.tecmint.com/10-useful-free-linux-ebooks-for-newbies-and-administrators/
|
||||
[8]:http://www.tecmint.com/free-linux-shell-scripting-books/
|
@ -0,0 +1,109 @@
|
||||
Pyinotify - 在 Linux 中实时监控文件系统更改
|
||||
============================================================
|
||||
|
||||
Pyinotify 是一个简单而有用的 Python 模块,它可用于在 Linux 中实时[监控文件系统更改][1]。
|
||||
|
||||
作为一名系统管理员,你可以用它来监视你感兴趣的目录的更改,如 Web 目录或程序数据存储目录及其他目录。
|
||||
|
||||
**建议阅读:** [fswatch - 监控 Linux 中的文件和目录更改或修改][2]
|
||||
|
||||
它取决于 inotify(内核 2.6.13 中包含的 Linux 内核功能),它是一个事件驱动的通知程序,其通知通过三个系统调用从内核空间导出到用户空间。
|
||||
|
||||
pyinotiy 的目的是绑定这三个系统调用,并在其上提供了一个通用和抽象的方法来操作这些功能。
|
||||
|
||||
在本文中,我们将向你展示如何在 Linux 中安装并使用 pyinotify 来实时监控文件系统更改或修改。
|
||||
|
||||
#### 依赖
|
||||
|
||||
要使用 pyinotify,你的系统必须运行:
|
||||
|
||||
1. Linux kernel 2.6.13 或更高
|
||||
2. Python 2.4 或更高
|
||||
|
||||
### 如何在 Linux 中安装 Pyinotify
|
||||
|
||||
首先在系统中检查内核和 Python 的版本:
|
||||
|
||||
```
|
||||
# uname -r
|
||||
# python -V
|
||||
```
|
||||
|
||||
一旦依赖满足,我们会使用 pip 安装 pynotify。在大多数 Linux 发行版中,如果你使用的是 Python 2 >= 2.7.9 或者从 python.org 下载了 Python 3 >=3.4 的二进制,那么 pip 就已经安装了,否则,就按如下安装:
|
||||
|
||||
```
|
||||
# yum install python-pip [On CentOS based Distros]
|
||||
# apt-get install python-pip [On Debian based Distros]
|
||||
# dnf install python-pip [On Fedora 22+]
|
||||
```
|
||||
|
||||
现在安装 pyinotify:
|
||||
|
||||
```
|
||||
# pip install pyinotify
|
||||
```
|
||||
|
||||
它会从默认仓库安装可用的版本,如果你想要最新的稳定版,考虑按如下从 git 仓库 clone 下来:
|
||||
|
||||
```
|
||||
# git clone https://github.com/seb-m/pyinotify.git
|
||||
# cd pyinotify/
|
||||
# ls
|
||||
# python setup.py install
|
||||
```
|
||||
|
||||
### 如何在 Linux 中使用 pyinotify
|
||||
|
||||
在下面的例子中,我以 root 用户(通过 ssh 登录)监视了任何用户 tecmint 家目录(/home/tecmint)下的改变,如截图所示:
|
||||
|
||||
```
|
||||
# python -m pyinotify -v /home/tecmint
|
||||
```
|
||||
[
|
||||
![Monitor Directory Changes](http://www.tecmint.com/wp-content/uploads/2017/03/Monitor-Directory-File-Changes.png)
|
||||
][3]
|
||||
|
||||
监视目录更改
|
||||
|
||||
接下来,我会观察到任何 web 目录 (/var/www/html/tecmint.com) 的更改:
|
||||
|
||||
```
|
||||
# python -m pyinotify -v /var/www/html/tecmint.com
|
||||
```
|
||||
|
||||
要退出程序,只要点击 `[Ctrl+C]`。
|
||||
|
||||
注意:当你在运行时没有指定要监视的目录是,`/tmp` 将作为默认目录。
|
||||
|
||||
在 Github 上了解更多 Pyinotify 信息:[https://github.com/seb-m/pyinotify][4]
|
||||
|
||||
就是这样了!在本文中,我们向你展示了如何安装及使用 pyinotify,一个有用的用于在 Linux 中监控文件系统更改的 Python 模块。
|
||||
|
||||
你有遇到类似的 Python 模块或者相关的[ Linux 工具/小程序][5]么?让我们在评论中了解,或许你也可以询问与这篇文章相关的问题。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
作者简介:
|
||||
|
||||
Aaron Kili 是 Linux 和 F.O.S.S 爱好者,将来的 Linux 系统管理员和网络开发人员,目前是 TecMint 的内容创作者,他喜欢用电脑工作,并坚信分享知识。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/pyinotify-monitor-filesystem-directory-changes-in-linux/
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/aaronkili/
|
||||
|
||||
[1]:http://www.tecmint.com/fswatch-monitors-files-and-directory-changes-modifications-in-linux/
|
||||
[2]:http://www.tecmint.com/fswatch-monitors-files-and-directory-changes-modifications-in-linux/
|
||||
[3]:http://www.tecmint.com/wp-content/uploads/2017/03/Monitor-Directory-File-Changes.png
|
||||
[4]:https://github.com/seb-m/pyinotify
|
||||
[5]:http://tecmint.com/tag/commandline-tools
|
||||
[6]:http://www.tecmint.com/author/aaronkili/
|
||||
[7]:http://www.tecmint.com/10-useful-free-linux-ebooks-for-newbies-and-administrators/
|
||||
[8]:http://www.tecmint.com/free-linux-shell-scripting-books/
|
Loading…
Reference in New Issue
Block a user