无需在 while 循环中使用变量,也无需将 grep 输出捕获到变量中 .
#!/bin/bash
pwd="/srv/scripts"
count=0
echo "Script Started on $(date -u) Failure.Count=$count" >> "$pwd/failures.count"
# No need for a variable here
while true; do
# Fix indentation
sleep 420
# Again, no need for a variable; use grep -q
if ! asterisk -rx "pjsip show registrations" | grep -q Registered
then
amportal restart
count=$(( $count + 1 ))
echo "Trunk Failure on $(date -u) Failure.Count=$count" >> "$pwd/failures.count"
fi
if [ "$count" -gt 5 ]; then
echo "Server Reboot due to Failure.Count=$count on $(date -u)" >> "$pwd/reboot.notification"
reboot
fi
done
我也许也会在一个日志文件中收集所有日志通知,并使用带有时间戳的更传统的日志格式,并且每个消息都使用脚本的名称 .
如果您看到成功,计数器是否应重置为零?由于您在错误的时间断开网络电缆而重新启动服务器似乎是您想要避免的 .