In general, you can use kill "$$"
from a subshell in order to terminate the main script ($$
will expand to the pid of the main shell even in subshells, and you can set a TERM
trap in order to catch that signal).
But it looks that you actually want to terminate the left side of a pipeline from its right side — i.e. cause inotifywait
to terminate without waiting until it’s writing something to the orphan pipe and is killed by SIGPIPE
. For that you can kill just the inotifywait
process explicitly with pkill
:
inotifywait -m /some/dir -e create,modify |
while read path action file; do
pkill -PIPE -P "$$" -x inotifywait
done
pkill -P
selects by parent; $$
should be the PID of your script. This solution is of course, not fool-proof. Also have a look at this.
CLICK HERE to find out more related problems solutions.