Article Title: Use inotify to monitor file system activities. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Inotify is a Linux? Feature, which monitors file system operations, such as reading, writing, and creating. Inotify is responsive, easy to use, and much more efficient than the busy polling of cron tasks. Learn how to integrate inotify into your application and discover a group of command line tools that can be used to further automate system management.
System Management is like daily life. Just like brushing your teeth and eating vegetables, daily maintenance can keep your machine in good shape. You must regularly clear waste, such as temporary files or useless log files, and spend time entering forms, replying to calls, downloading updates, and monitoring processes. Fortunately, automated shell scripts, monitoring using Nagios and other tools, and task scheduling using common cron can reduce this burden.
But strangely, none of these tools are responsive. Of course, you can schedule a frequently-run cron task to monitor the conditions, but such a busy round robin consumes a lot of resources and is uncertain-is not ideal. For example, if you need to monitor several Transfer Protocol (FTP) storage boxes of input data, you may need to use the find command to scan each target directory and list new contents. However, although this operation does not seem to have any harm, each call generates a new shell and find command, which requires many system calls to open the directory, then scan the directory, and so on. This may cause too frequent or a large number of polling tasks (worse, busy polling is not always good. Imagine a file system browser, such as the Mac OS X Finder, to poll for a large amount of resources and their complexity when updating ).
What should the Administrator do? Fortunately, you can turn to trusted computers again.
Learn more about inotify
Inotify is a Linux kernel feature that monitors the file system and sends related event warnings to specialized applications in a timely manner, such as delete, read, write, and uninstall operations. You can also track the source and target details of an activity.
Using inotify is simple: create a file descriptor, append one or more monitors (one monitor is a path and a group of events), and then use the read () method to obtain event information from the descriptor. Read () does not use up the entire cycle. It is blocked before the event occurs.
Better yet, because inotify works with traditional file descriptors, You can passively monitor monitors and many other input sources using conventional select () system calls. Both methods-blocking file descriptors and using select ()-avoid busy polling.
Now, let's take a closer look at inotify, write some C code, and then look at a group of command line tools that you can build and use to attach commands and scripts to file system events. Inotify does not lose control in the middle, but it can run cat and wget and strictly execute it if necessary.
To use inotify, you must have a Linux machine with 2.6.13 or a kernel update (earlier Linux kernel versions use the lower-level File Monitor dnotify ). If you do not know the kernel version, go to shell and enter uname-:
% uname -aLinux ubuntu-desktop 2.6.24-19-generic #1 SMP ... i686 GNU/Linux |
If the listed kernel version is no less than 2.6.13, your system supports inotify. You can also check the/usr/include/sys/inotify. h file of the machine. If it exists, your kernel supports inotify.
Note:: FreeBSD and Mac OS X provide a kqueue similar to inotify. Enter man 2 kqueue on the FreeBSD machine to obtain more information.
This article is based on Ubuntu Desktop version 8.04.1 (that is, hard), which runs on Parallels Desktop version 10.5 of Mac OS X version 3.0 Leopard.
[1] [2] [3] [4] Next page