You can ask the program not to quit, by the way, but that makes scripting a little more difficult. The script just takes advantage of this behavior to set the FN variable, takes action, and then relaunches inotifywait. The name of the file will appear in the first terminal and the program will exit. Then in the other terminal create a file in the same directory. Don’t forget the period at the end which tells it to monitor the current directory. In the lower terminal, issue the inotifywait command. ![]() If you want to try the command without a script so you can see the effect, just open up two terminal windows like this: Only when the file is done will it rename (move) the file to test.txt. For example, Chrome will download the file test.txt as or something like that. Usually, a new file doesn’t have the final name until it is complete. If you are wondering why the move case is necessary, think about how most text editors and network download software works. There are other events you can wait for as well, of course. Either way will work and the %f tells the command to report the file name. I figure any sane program putting something in the directory will either open the file for writing and close it, or it will move it. It doesn’t wake up frequently, only when something has changed. ![]() If FN=`inotifywait –e close_write,moved_to -format %f. One of those tools is inotifywait and it makes for a nicer script. There is also a set of command-line programs you can install, usually packaged as inotify-tools. You can use these calls programmatically with the sys/inotify.h header file. Honestly, you want something more elegant right? Modern kernels (2.6.13 and later) have filesystem notifications in the form of an interface called inotify. (If you think I should use for I in *, try doing that in an empty directory and you’ll see why I use the ls command instead.) This is really not a good script because it executes all the time and it just isn’t a very elegant solution. Just for an example, I dump the file to the console and remove it, but in real life, you’d do something more interesting. Here’s a really dumb shell script: #!/bin/bash The simple but ugly way to do this just scans the directory periodically. For example, you might like to watch a directory and kick off some program automatically when a file appears from a completed FTP transaction, without having to sit there and refresh the directory yourself. But sometimes you want the system to react to changes in the system without your intervention. If you are only interested in create/rename/delete events (like most UIs are) you only need to call to see if a file has been created/renamed/deleted since you last checked.The UNIX Way™ is to cobble together different, single-purpose programs to get the effect you want, for instance in a Bash script that you run by typing its name into the command line. Not a complete solution, but on the systems where returns the last update of a directory/file a reasonably efficient solution is just glob all files in a directory tree and storing their mtime. filewatcherd FreeBSD daemon Generic Solution In both cases watchman returns JSON output. It can be controlled using the command line or a JSON API spoken over a UNIX socket. watchman A service that can watch for changes on Linux, OS X, FreeBSD, OpenBSD and Illumos/Solaris and abstracts away the differences between those platforms. Like the rest of TWAPI, WIndoes NT 4.0 and later only. See Also directory notification package in Tcl for Linux Tcl-Inotify an efficient Linux-only solution TWAPI has the begin_filesystem_monitor/cancel_filesystem_monitor functions to allow you to monitor file system changes by registering a callback. Pat Thoyts quickly found FindFirstChangeNotification and friends on MSDN and created a working extension. It'd also be nice to have this functionality for a lot of platforms. The quick consensus on the chat was that there wasn't one. ![]() I asked if anyone knew of an extension for win32 that uses the directory notification API of windows. File and directory change notifications/events are useful for (G)UIs showing a list of files, for mirroring directory(trees), tail applications, and some other things I can't think of right now.
0 Comments
Leave a Reply. |