Initially, I tried to run inotifywait
in monitor mode, like this:
for i in $(seq 1 3)
do
echo "stage $i"
train_model &
pid=$!
inotifywait -mq -e close_write --format '%f' $MODEL_SAVE_PATH | while read FILE
do
echo "[`date "+%F %T"`] new model: $FILE"
if ps -p $pid > /dev/null # train_model is finished
then
kill -2 -$$
done
done
done
Sadly, the kill -2 -$$
command will kill the whole script, which means it won't run the remaining loops.
I also tried to run the inotifywait
part in a subshell, still got no luck.
So, I need to use a for loop to control how many times inotifywait
should be executed in each stage, which is ugly, like this:
for i in $(seq 1 3)
do
echo "stage $i"
train_model &
for _ in $(seq 100)
do
inotifywait -q -e close_write --format '%f' $MODEL_SAVE_PATH | while read FILE
do
echo "[`date "+%F %T"`] new model: $FILE"
done
done
done
In the first solution, ideally, inotifywait
will exit when train_model
is finished, this is exactly what I want. In the second solution, I need to rely on an extra variable, which feels redundant. However, before I find a more elegant way to achieve this, I'm afraid I'll be stuck with solution 2 for some time :(