I’m running an inotifywait-loop-script managed by procd.
My procd init file looks like:
USE_PROCD=1
PROG=/tmp/inotifywait-test
start_service() {
procd_open_instance
procd_set_param command $PROG
procd_close_instance
}
The first version of inotifywait-test -- which is happily starting but not stopping everything/correctly -- is:
inotifywait -m -e close_write /tmp |
while read path action file
do
echo "${path}/${file} completed"
done
ps | grep inotify after start:
30738 root 1204 S {inotifywait-tes} /bin/sh /tmp/inotifywait-test
30744 root 948 S inotifywait -m -e close_write /tmp
30746 root 1204 S {inotifywait-tes} /bin/sh /tmp/inotifywait-test
30766 root 1204 S grep inotifywait
and after stop:
30744 root 948 S inotifywait -m -e close_write /tmp
30746 root 1204 S {inotifywait-tes} /bin/sh /tmp/inotifywait-test
30794 root 1204 S grep inotifywait
The script not correctly stopping is due to the pipe (|) opening a sub-shell, not being implicitly terminated when the parent is. That caveat and a solution is well documented here: https://openwrt.org/docs/guide-developer/procd-init-scripts#a_common_gotcha_with_stopping_a_service
So we adapt mentioned solution and inotifywait-test is now looking like this:
pid=
_cleanup() {
kill $pid
exit
}
trap _cleanup TERM INT
inotifywait -m -e close_write /tmp |
while read path action file
do
echo "${path}/${file} completed"
done &
pid=$!
wait $pid
ps | grep inotify after start:
30875 root 1204 S {inotifywait-tes} /bin/sh /tmp/inotifywait-test2
30886 root 948 S inotifywait -m -e close_write /tmp
30887 root 1204 S {inotifywait-tes} /bin/sh /tmp/inotifywait-test2
30905 root 1204 S grep inotify
and after stop:
30886 root 948 S inotifywait -m -e close_write /tmp
30933 root 1204 S grep inotify
so apparently "more" got stopped now compared to v1, but the inotifywait-process itself is still running.
What am I missing? Is there another level nested within, which also needs cleanup?
"The internet"(TM) says I should kill the PID-group e.g. via kill SIGTERM -$$, however I did not manage to actually stop the script from within procd so far.