inotifywaitスクリプトはファイルを無期限に監視し、プロセスを再開します。アドバイスが必要です。

inotifywaitスクリプトはファイルを無期限に監視し、プロセスを再開します。アドバイスが必要です。

Inotifyを使用してアプリケーション構成ファイルを作成するときにプロセスを再起動するスクリプトを作成しようとしています。スクリプトは、実行しているホストによって異なる動作をする必要があります。 「プライマリ」サーバーで実行している場合、スクリプトはプロセスを再起動し、ファイルを別の「セカンダリ」ホストにscpする必要があります。 「セカンダリ」サーバーで実行している場合は、プロセスを再起動する必要があります。

バックグラウンドでスクリプトを実行していますが、しばらくしてスクリプトプロセスが終了し、セカンダリホストでスクリプトがプロセスを再起動し、1回ではなく複数回実行されることを確認しました。

    #!/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 "$@"

私は何が間違っていましたか?

ベストアンサー1

おすすめ記事