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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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