Exiting entire bash script from subshell

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật