Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to automatically run a script whenever new files are copied into a particular directory. In other words, is there a way in Linux to "watch" a directory for changes and then run something in response to the change?

share|improve this question
add comment

4 Answers

up vote 13 down vote accepted

If you're lucky enough to be on a debian-based distribution, apt-get install dnotify. Other distributions probably have something similar - look for the dnotify name.

dnotify is a simple program based on Linux kernel 2.4.19+'s dnotify API. dnotify can execute a specified command each time the content of a specific directory changes. It is run from the command line and takes two arguments: one or more directories to monitor and a command to execute whenever a directory has changed. Options control what events to trigger on: when a file was read in the directory, when one was created, deleted and so on.

If you want to handle this within your own program, dnotify is also the API you want to use.

share|improve this answer
    
Yes, I'm lucky enough to use Debian. ;-) Thanks! –  GeneQ Aug 5 '09 at 2:11
3  
@MikeB, after Googling dnotify you suggested, I ended up using inotify instead, which apparently superceeds it. Thanks for pointing me the right direction. apt-get install inotify-tools –  GeneQ Aug 5 '09 at 2:21
6  
@GeneQ, inotify has replaced dnotify. –  David Aug 5 '09 at 2:48
1  
Likewise on Gentoo: emerge inotify-tools –  James Sneeringer Aug 5 '09 at 4:31
    
Yeah, I was going to say, kernel 2.4.19 is kind of ancient ;-) inotify is the way to go. –  David Z Aug 5 '09 at 5:50
show 2 more comments

You can run a script with the inotify-tools, something like this. It will watch the directory for changes in modified files, new files, and deleted files, then it will execute the script.

#!/bin/sh
while inotifywait -e modify -e create -e delete /home/me/code; do
    rsync [options] /home/me/code/ /media/nfs/code/ 
done
share|improve this answer
    
Just take into account that if the source directory changes while it is being copied, inotifywait will NOT see it and those changes will not be copied until further changes once the copy has finished. Probably not an issue though. –  esperanto May 16 '13 at 9:30
add comment

incron is basically what you want, I think. It uses inotify as the notification mechanism (which, as others have pointed out, supercedes dnotify), but doesn't require a script that continuously runs, using inotifywait or similar (although, obviously, the incron daemon is running all the time). System-wide 'crontabs' and user 'crontabs' are supported in a similar way to standard cron, but rather than specifying times as triggers, inotify events and files/directory names are used.

incron is packaged for many distributions, including Ubuntu and Debian, I believe.

share|improve this answer
add comment

There's a piece of software solely for this purpose, autoenv You might want to check it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.