I’m a bit new to bash scripting, I have a C++ program communicating back and forth with this bash script through some named pipes. I used inotifywait to watch a folder for new files and when a new file is added (ending in .job) sending it to through the pipe.
I’m having the C++ program pipe back the result, and if the result is ‘quit’, I want to bash script to quit execution.
I was trying to accomplish this with exit 1 as seen below, but that doesn’t seem to exit the entire script. Instead after that exit is ran, when I drop another file in the watch folder the script ends.
I read a bit about subshells, and am wondering if this has something to do with them and any suggestions on how to exit the entire script.
DROP_FOLDER="$1"
DATA_FOLDER="$2"
OUTPUT_FOLDER="$3"
PATH_TO_EXECS="./tmp/"
PATH_TO_COMPLETED="../completed/"
# create pipes
read_pipe=/tmp/c_to_bash
write_pipe=/tmp/bash_to_c
if [[ ! -p $read_pipe ]]; then
mkfifo $read_pipe
fi
if [[ ! -p $write_pipe ]]; then
mkfifo $write_pipe
fi
# start c++ program
./tmp/v2 $DATA_FOLDER $OUTPUT_FOLDER $PATH_TO_EXECS "${write_pipe}" "${read_pipe}" &
# watch drop folder
inotifywait -m $DROP_FOLDER -e create -e moved_to |
while read path action file; do
# ends in .tga
if [[ "$file" =~ .*tga$ ]]; then
# move to image dir
mv "${DROP_FOLDER}${file}" "${DATA_FOLDER}${file}"
fi
# ends in .job
if [[ "$file" =~ .*job$ ]]; then
# pipe to dispatcher
echo "${DROP_FOLDER}${file}" > $write_pipe
# wait for result from pipe
if read line <$read_pipe; then
echo $line
# check for quit result
if [[ "$line" == 'quit' ]]; then
# move job file to completed
mv "${DROP_FOLDER}${file}" "${PATH_TO_COMPLETED}${file}"
# exit
exit 1
fi
# check for continue result
if [[ "$line" == 'continue' ]]; then
# move job file to completed
mv "${DROP_FOLDER}${file}" "${PATH_TO_COMPLETED}${file}"
fi
fi
fi
done
0
The problem is that exit
only exits the current subshell, which in your case is your while loop due to the pipeline.
Bash still waits for inotifywait
to exit, which it won’t do until it tries to write another value and detects that the pipe is broken.
To work around it, you can use process substitution instead of a pipe:
while read path action file; do
...
done < <(inotifywait -m $DROP_FOLDER -e create -e moved_to)
This works because the loop is not executed in a subshell, and therefore an exit
statement will exit the whole script. Additionally, bash doesn’t wait for process substitutions to exit, so while it may hang around until the next time it tries to write, it won’t stop the script from exiting.
1
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.