Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want a script to sleep unless a certain file is modifed/deleted (or a file created in a certain directory, or ...). Can this be achieved in some elegant way? The simplest thing that comes to my mind is a loop that sleeps for some time before checking the status again, but maybe there is a more elegant way?

share|improve this question

2 Answers

up vote 22 down vote accepted

On linux, you can use the kernel's inotify feature. Tools for scripting can be found there: inotify-tools.

Example use from wiki:

#!/bin/sh

EVENT=$(inotifywait --format '%e' ~/file1) # blocking without looping
[ $? != 0 ] && exit
[ "$EVENT" = "MODIFY" ] && echo 'file modified!'
[ "$EVENT" = "DELETE_SELF" ] && echo 'file deleted!'
# etc...
share|improve this answer
Most unices have a similar feature. Unfortunately each has its own interface, and many have no shell API. – Gilles Jan 7 '11 at 18:40

There is an API called inotify for C programmers.

There are some tools that use it, e.g. incron and inotify-tools.

share|improve this answer
Tremendous, that’s the stuff. Cheers Mikel! – Paul D. Waite Apr 6 '11 at 8:52

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.