I’m not a big fan of GUI editors, but I know I’m in the minority. You can find it all in the incron man pages. You can monitor all the usual events and also set options to do things like not dereference symbolic links. You can also grab the vent time as text ( $%) or a number ( $&). MONITOR DIRECTORY FOR NEW FILES LINUX FULLHere’s a line from the file that will do the job: /home/alw/Downloads/hexfiles IN_CLOSE_WRITE,IN_MOVED_TO /home/alw/bin/program_cpu at the end provides a full path to the file affected. The format is very picky (it wants spaces, not tabs, for example). You can use the command incrontab -e to edit your incron table. Suppose you want to run a script when a file appears in the hexfiles directory. MONITOR DIRECTORY FOR NEW FILES LINUX INSTALLOnce you install it, you will probably have to change /etc/incron.allow and /etc/ny if you want to actually use it, especially as a normal user. The incron program is like cron but instead of time-based events, the events are based on file notifications. There is another program for that, called incron (you will almost surely have to install this one). You really don’t want to repeat this script, or a variation of it, for each case. Presumably, a system might have lots of different directories it wants to monitor. The script is still less than ideal, though. If you think you need that capability, you can read the man page. The other command line, inotifywatch, also outputs file change events but it watches for a certain amount of time and then gives you a summary of changes. However, it also removes the problem of a file changing while you are doing your processing. 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. MONITOR DIRECTORY FOR NEW FILES LINUX DOWNLOADFor 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. MONITOR DIRECTORY FOR NEW FILES LINUX SOFTWAREIf 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. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |