
私が書いたこの関数は、OSXとLinuxコマンドの間の特性ping
、特に-W
タイムアウトオプションを反映するはずです。
# Checks if a host is up-and-running and responding to pings
function my_function_is_network_host_up() {
local -ri N_ARGUMENTS=1
if ! check_n_arguments "${FUNCNAME}" "${N_ARGUMENTS}" "${@}"; then return; fi
local -r NETWORK_HOST=$1
local -ri n_requests=1
local -i wait_for_reply=1 # Seconds
if [[ $my_global_os_type == 'OSX' ]]; then
((wait_for_reply *= 1000)) # Milliseconds
fi
ping -c "$n_requests" \
-W "$wait_for_reply" \
-q \
"$NETWORK_HOST" \
&> /dev/null
return $?
}
実際のシステム管理者は、ジャンプしたシステムとは別にホストをチェックするために、より良いBash機能を使用しますか?
私の一般的な使用法:
hs=(...);
for h in "${hs[@]}"; do
if my_function_is_network_host_up $h; then
do_stuff
fi
done
それ以外の場合:
my_function_is_network_host_up $h && do_stuff
背景
OSX
Time in milliseconds to wait for a reply for each packet sent. If a reply arrives later, the packet is not printed as replied, but considered as replied when calculating statistics.
Linux
Time to wait for a response, in seconds. The option affects only timeout in absense of any responses, otherwise ping waits for two RTTs.
答え1
あなたの機能が何であるかを理解していません。単純な走りに比べてどのような利点がありますか?
for host in host1 host2 host3; do ping -c 1 host >/dev/null && do_stuff; done
ホストが動作していることを確認し、ホストの起動時にコマンドを実行するには、上記の内容で十分です。