Linux file folder recursive monitoring

Source: Internet
Author: User
Linux file folder recursive monitoring

Linux file folder recursive monitoring

A standard php composer package is developed. you can directly use require when using it.

Implementation

The implementation of the php version follows the idea of the shell version. it uses pipeline commands to pass the results of shell commands, and then processes the results to achieve our purpose of monitoring files.

To complete this function, you must first implement a php-version pipeline command. here I encapsulate resource popen (string $ command, string $ mode, the return value of a command can be processed in a friendly way.

After completing the above modules, the next step is the specific implementation. here there are multiple friendly methods to add multi-path recursive monitoring, including regular matching, and exclude regular matching.

Pipe development

 Command = $ cmd; return $ this;}/*** sets the callback function to process the pipeline output command */public function setCallback (callable $ cb) {$ this-> callback = $ cb; return $ this;}/*** sets the delimiter between data fragments */public function setDelimiter ($ delimiter) {$ this-> delimiter = $ delimiter; return $ this;}/*** start running */public function run () {$ fp = popen ($ this-> command, "r"); if (false ===$ fp) {throw new \ RuntimeException ("popen execute command fail Ed! ") ;}$ Item =" "; while (! Feof ($ fp) {$ char = fgetc ($ fp); if ($ this-> delimiter ==$ char) {call_user_func ($ this-> callback, $ item); $ item = "";} else {$ item. = $ char ;}} pclose ($ fp );}}

Below is the test program

 Include "vendor/autoload. php "; 

$ Test = new \ Ritoyan \ Pipe ();
$ Test-> setCmd ("tail-f/root/t.txt")-> setCallback (function ($ item ){
Echo "get content:". $ item. "\ n ";
})-> SetDelimiter ("\ n")-> run ();

Real-time processing of the returned results of the shell command tail-f/root/t.txt. the callback function is set for setCallback (callable func). The func parameter is the standard output of the shell command, setDelimiter ($ delimiter) sets the delimiter for the input callback function parameter, so that the output can be easily and arbitrarily passed to the callback function.

Inotify development

This part of the code will not be raised, mainly relying on the above Pipe and then processing the output of the inotifywait-mrq -- format '% w, % e, % f' command, forward and reverse filtering, the following is the call to Inotify:

 AddExclude (["/swp $/", "/swpx $ /","/~ $/","/\ D $/","/swx $/"])-> setCallback (function ($ item) {echo $ item [" event "]. "File ". $ item ["file"]. "\ n" ;})-> addPaths ("/datas/git/")-> start ();

After this operation, the following content will be output when we modify the files in the/datas/git directory, which makes it easy to customize the modified files.

CREATE file/datas/git/inotify/README. mdMODIFY file/datas/git/inotify/README. mdMOVED_TO file/datas/git/aizuyan/pinyin-1/README. mdDELETE file/datas/git/aizuyan/pinyin-1/LICENSE ......

Install inotify-tools

The entire function depends on a linux software-inotify-tools
Centos install yum install inotify-tools or install the tool directly in the OS through the source code ~

How to use

I have released him to the composer repository and can easily install it:
Composer require aizuyan/inotify, which can be used as in the preceding example.

In addition, this is the github address of the two developed components: Aizuyan \ Pipe, Aizuyan \ Inotify

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.