I need to create a shell script that executes an application when a file called
start is created in a particular directory.
How may I write a shell script that waits for the creation of a file with a certain name in a certain location?
If your system has
inotifywait (most Linux distributions, OpenBSD and possibly others), as others have mentioned the least resource-intensive approach is to use
rm -f yourdirectory/start while [ "$(inotifywait -e create -q --format %f yourdirectory)" != "start" ]; do :; done yourcommand
That will ensure the
start file is removed from
yourdirectory, then wait for its creation, and then run
-e create specifies that we’re only interested in creation events,
-q specifies that we don’t want
inotifywait’s diagnostic messages, and
--format %f specifies that we only want to see the created file’s name in the output.
If you don’t have
inotifywait, the following will work too, with a delay of at most one second:
rm -f yourdirectory/start while [ ! -e yourdirectory/start ]; do sleep 1; done yourcommand
I like the tool 'inoticoming' for watching a directory for change. has logging and pid-file creation. you can run command on change with additional 'action-options' it's pretty decent :)
i also remember a tool called 'fileschanged' but never got to play with it, could be worth a look. -Jay