I am having an issue with exiting a bash script out of a while loop:while read -r dir event name; do  case $event in    OPEN)    chown $VHOST:$VHOST $WEBPATH/$name;    echo The file \$name\ was created (not necessarily writable);    ;;    WRITE)    echo The file \$name\ was written to;    ;;    DELETE)    echo The file \$name\ was deleted;    exit 0;    ;;  esacdone < <(/usr/bin/inotifywait -m $WEBPATH)The loop correctly listens for file changes in the given Directory, so far so good.This also shows on the console output:root #: bash /var/scriptusr/letsencrypt/dir-changeSetting up watches.Watches established.The file tes was created (not necessarily writable)The file tes was deletedroot #:Apparently it seems the script exited nicely but when you search for it in the process tree it is still there:root #: ps aux | grep dir-root      5549  0.0  0.0  14700  1716 pts/0    S    14:46   0:00 bash /var/scriptusr/letsencrypt/dir-changeroot      5558  0.0  0.0  14184  2184 pts/1    S+   14:46   0:00 grep dir-root #:So my question is how to really exit the script?
Breaking out of while loop with a switch case inside
bash;loop device
I came up with a solution after searching for a bit.The problem originates from inotifywait going subshell as @mikeserv stated in the comments above.So I had to write a cleanup method for it. My script:#!/bin/bash### script for immediatly changing the owner and group of the let's encrypt challenge file in the given webrootPidfile=$( cd $( dirname ${BASH_SOURCE[0]} ) && pwd )/run-file-chowner.pidecho $$ > $Pidfilefunction terminate_process () {        trap - SIGHUP SIGINT SIGTERM SIGQUIT        printf \nTerminating process...\n        rm $Pidfile > /dev/null 2>&1;        kill -- -$$        exit $1}function main () {trap terminate_process SIGHUP SIGINT SIGTERM SIGQUITlocal OPTIND D optwhile getopts D: opt;do        case $opt in        D)                Domain=$OPTARG;;        esacdoneshift $((OPTIND-1))case $Domain in        'domain-b.com')                VHost=doma-www        ;;        'domain-a.com')                VHost=domb-www        ;;        *)                printf \nScript usage : [ $0 -D \example.com\ ]\n\n                exit 1;        ;;esacWebPath=/var/www/$Domain/$VHost/htdocs/public/.well-known/acme-challengeinotifywait -m $WebPath | while read -r dir event name; do        case $event in        CREATE)                chown $VHost:$VHost $WebPath/$name                printf \nOwner and group of \$name\ were changed to $VHost...\n        ;;        DELETE)                printf \nThe file \$name\ was deleted\n                terminate_process 0        ;;        *)                printf \nEvent $event was triggered.\n        ;;        esacdone}main $@In this is the output, when a file in the watched folder is created and deleted:root #: bash file-chowner -D dom-a.comSetting up watches.Watches established.Owner and group of test were changed to doma-www...Event OPEN was triggered.Event ATTRIB was triggered.Event CLOSE_WRITE,CLOSE was triggered.Event ATTRIB was triggered.The file test was deletedTerminating process...TerminatedTerminating process...Terminated