I'm writing a bash script to use inotifywait to monitor a directory and kick off actions when changes are detected. Something like:
inotifywait -m ... | while read f; do something; done
Since inotifywait doesn't terminate by itself, this script will not halt.
So my plan was to get the PID of the inotifywait process, save it to a file and have a different process kill it later, say like:
inotifywait -m ... | { echo ??PID?? > pid-file; while ... }
But I don't know how to get the PID. Is there a simple way to achieve this? Another way is just to save the PID of the shell-script $$ to the file and kill the entire shell-script but I wanted to do some cleanup after the while loop.
I have tried using coproc and I think it will work but it seems like more complication than necessary.