Results 1 to 5 of 5
I have a script I would like to run every time a specific folder changes. I want this to be run the instant the folder is changed every time it ...
- 05-20-2010 #1Just Joined!
- Join Date
- May 2010
- Posts
- 3
[SOLVED] Running a script when a folder changes
I have a script I would like to run every time a specific folder changes. I want this to be run the instant the folder is changed every time it is changed. I have found some information about inotify and it sounds like it would probably do what I want, but am unsure if it can be made to watch only a single folder rather than the whole file system, and I'm unsure of how quickly it works. Does anyone have any thoughts on this?
- 05-20-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
The inotify subsystem is very fast. The command 'inotifywait' allows you to specify a file or directory, or multiples, and there is a recursive option that will look for any changes in sub-directories as well; however, it is not recursive by default. You can use this in a script that loops on the wait and then takes some action when the directory changes.
As to exactly how quickly it works, well that depends upon the load on the system more than anything else I suspect. I have played with inotify, but I am not yet using it in production systems because it only works on local file systems, at least in the environment I was testing it on. In that case, I needed to detect changes on a remote NFS or CIFS file system, and it wasn't doing so, but that was on an embedded ARM system running Debian Etch with a 2.6.21 kernel. I haven't tried it on my CentOS workstation/server yet.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 05-20-2010 #3Just Joined!
- Join Date
- May 2010
- Posts
- 3
After my original post I continued researching inotify and found some information on inotifywait. I thought it sounded like that was what I needed, and your feedback has helped me confirm that. I am still looking for good syntax notation so that I can write a script to make use of it. I have found some examples but have not yet found anything that fully explains the syntax. I suppose I should attempt to read over the man page if I can't find anything else.
- 05-20-2010 #4
please post your results, I'm interested in the results.
- 05-21-2010 #5Just Joined!
- Join Date
- May 2010
- Posts
- 3
So far I haven't looked much more into it. I installed inotify-tools so that I can make use of inotifywait, but I haven't had time to read up on how to write a script using inotifywait yet. I'll keep you updated when I make more progress.
UPDATE: I have been thinking of the possible downsides of the way I was thinking about implementing this script and after looking at the man page of inotifywait I have decided I will watch a single file instead of a folder. I will just use the folder as a way of easily telling the server to run the script without entering an ssh session. This seems like a bad way of doing what I want to achieve but since I'm already researching inotifywait I figure it would be good to make it work just for the experience. After I'm done with this I think I will instead try to figure out how to make a script that will simply log into an ssh session and run the script for me. This seems like a more practical way of doing it.
UPDATE2: Okay, so after spending some time experimenting with inotify, I decided that it doesn't quite work the way I need it to in this case. The applications I was trying to use with it seem to act kind of unpredictably so I'm not sure I can make inotify work 100 percent of the time. Instead I have decided to use a batch file that runs the command line version of the putty ssh client for windows and runs the script for me. The main reason I was doing this is because I didn't want to SSH into my linux machine every time I wanted to run this script, but having a script on my windows machine that does that for me works just fine for my purposes.


