简体   繁体   中英

Trigger a process on change of a file in unix

I need to trigger a process in remote node when a file changes in different remote node. Scenario is like this. There is a cron running at an unknown time in a remote node. Cron triggers some process which will change some files in the same node. What I need to do is that, when those files change I need to perform parsing of those files and populate it in database in someother remote node.

How can I make this happen. How to get to know when the file changes in a node and trigger a .sh file when any change is found in a different node.

java7 WatchService meet your requirements, code as follow

    WatchService watchService=FileSystems.getDefault().newWatchService();  
    Paths.get("/opt").register(watchService,   
            StandardWatchEventKinds.ENTRY_CREATE,  
            StandardWatchEventKinds.ENTRY_DELETE,  
            StandardWatchEventKinds.ENTRY_MODIFY);  
    while(true)  
    {  
        WatchKey key=watchService.take();  
        for(WatchEvent<?> event:key.pollEvents())  
        {  
            System.out.println(event.context()+" happen "+event.kind());  
        }  
        if(!key.reset())  
        {  
            break;  
        }  
    }

You should look at incrontab http://linux.die.net/man/5/incrontab which runs commands based on file events.

It is based on inotify syscall ( http://man7.org/linux/man-pages/man7/inotify.7.html ), which provides a mechanism for monitoring filesystem events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM