I'm trying to write a script that will restart a process when the application configuration file is written to, using inotify.The script is supposed to act differently depending on which host it is run:If it is run on a primary server, the script should restart the process, and  scp the file to the other, secondary hosts.If it is run on a secondary server, it should just restart the process.I run the script in the background and I noticed the script's process dies after a while, and on the secondary hosts, the scripts restart process runs a number of times, instead of just once.    #!/bin/bash# Script to watch APP application file for write changes.# If APP is written to and saved, on server0, restart APP and check if its running.# Then push to other servers with userone account.# If not on server0, push simply restart APP.declare -a APP_HOSTS=(server0 server1 server3 pushserver0 pushserver1 pushserver3 vumitest)declare -a my_needed_commands=(/opt/APP/conf/application.conf inotifywait service)HOSTNAME=$(hostname -s)missing_commands(){    local missing_counter=0    for needed_command in ${my_needed_commands[@]}; do        if ! command -v $needed_command >/dev/null 2>&1; then            printf Command not found in PATH: %s\n $needed_command >&2            ((missing_counter++))        fi    done    if ((missing_counter > 0)); then        printf Minimum %d commands are missing in the PATH, aborting.\n $missing_counter >&2        exit 1    fi}restart_and_scp(){    local host_count=0    local restofthem=(${APP_HOSTS[@]})    unset restofthem[0]    if service APP restart >/dev/null 2>&1; then        for each in ${restofthem[@]}; do            scp -Cqv ${my_needed_commands[0]} userone@$each:/opt/APP/conf >/dev/null 2>&1            ((host_count++))            if [ host_count -eq ${#restofthem[@]} ]; then                break            fi        done    fi}restart_APP(){    service APP restart >/dev/null 2>&1}prime_watch(){    inotifywait -mrq -e close_write ${my_needed_commands[0]} | \    while read filename; do restart_and_scp; done}other_watch(){    inotifywait -mrq -e modify ${my_needed_commands[0]} | \    while read filename; do restart_APP; done}setup_watch(){    if [ ! -z $1 ]; then        prime_watch     else        other_watch    fi}main(){    if [[ ${APP_HOSTS[0]} =~ ${HOSTNAME} ]]; then         setup_watch prime    else        setup_watch    fi     exit}main $@Please what am I doing wrong?
inotifywait script to watch file indefinitely, and restart process needs advice
linux;bash;inotify