Under Linux, you can use the inotify kernel subsystem to efficiently wait for the appearance of a file in a directory:
inotifywait -e create,moved_to,attrib --include '/sleep\.txt$' -qq /tmp
# script execution continues ...
NB: I included the attrib event to also register touch /tmp/sleep.txt when the file already exists.
In cases where there is a race condition between sleep.txt showing up and the inotifywait invocation, i.e. when the file might be created or touched just before the watch is established - and then never again, afterwards - one can extend the code like this (here assuming a recent version of the bash shell):
coproc inw {
LC_ALL=C exec inotifywait -e create,moved_to --include '/sleep\.txt$' /tmp 2>&1
}
while IFS= read -r -u "${inw[0]}" line; do
if [ "$line" = "Watches established." ]; then
break
fi
done
if [ -e /tmp/sleep.txt ]; then
kill "$inw_PID"
else
wait
fi
# script execution continues ...
The advantage of this approach in comparison to fixed time interval polling like in
until [ -e /tmp/sleep.txt ]; do sleep 1; done
# script execution continues ...
is that your process sleeps more. With an inotify event specification like create,moved_to,attrib the script is just scheduled for execution when a file under /tmp is created or has its attributes modified. With the fixed time interval polling you waste CPU cycles for each time increment.
$MAILPATH.wakeup.txtorterminate.txtinstead ofsleep.txtperhaps would be a more obvious choice for signaling your script to continue with its execution ... after it was in a state of sleep ...