Exiting entire bash script from subshell

Adam Jean-Laurent

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
user414777

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.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Script not exiting on error bash ios

分類Dev

exit a bash command in a script without exiting the script

分類Dev

Exiting bash script for loop on expect script expect error

分類Dev

Python: Exiting script

分類Dev

Script exiting on failed nosetests

分類Dev

Wait for the subshell in Bash IO redirection

分類Dev

Julia from bash script

分類Dev

Keep process running on remote machine after exiting ssh session inside bash script

分類Dev

Does a bash subshell spawn a new `bash` process?

分類Dev

Is $() a subshell?

分類Dev

Python Script not exiting with keyboard Interrupt

分類Dev

Run bash script from php

分類Dev

the difference between a subshell and detach in bash or zsh?

分類Dev

Bash run command in background inside subshell

分類Dev

Iterate over the output of a command in bash without a subshell

分類Dev

Forking and exiting from child in python

分類Dev

bash: exit from source-d script

分類Dev

Execution CMake from within a Bash Script

分類Dev

bash run curl from another script

分類Dev

Removing strings from file using bash script

分類Dev

Tokenize string from $REPLY in bash script

分類Dev

Call a .bashrc function from a bash shell script

分類Dev

math operations from array elements in bash script

分類Dev

How to read subshell output as each line is generated in bash

分類Dev

Changing the PS1 on an interactive bash subshell easily

分類Dev

google sheets - convert data in entire column from KB to GB using google script

分類Dev

Run bash script from curl failed while from file succeeded

分類Dev

Prevent grep from exiting in case of nomatch

分類Dev

docker stop spark container from exiting

Related 関連記事

ホットタグ

アーカイブ