用bash计算一天中出现的次数?

时间:2015-01-05 05:04:00

标签: bash shell

我有一个脚本,当我的互联网断开连接时自动连接我的调制解调器并在发生这种情况时写一个日志,我的脚本看起来像这样:

#!/bin/bash
DATE=`date +%d-%m-%Y_%H:%M:%S`
echo -n $DATE >> /www/restart.log
echo ' Internet terkoneksi ke server OpenVPN' >> /www/restart.log
sleep 20
while [ true ]
do
    if tail -1 /www/vpn.log | grep 'Initialization Sequence Completed'
    then
        sleep 5
    else
        profile start > /dev/null 2>&1 &
        DATE=`date +%d-%m-%Y_%H:%M:%S`
        echo -n $DATE >> /www/restart.log
        echo ' Openvpn Disconnected. Koneksi internet direstart' >> /www/restart.log
        echo '---------------------------------------------------------------------------------------------------'      >> /www/restart.log
        rm /www/vpn.log
        break
    fi
done

脚本在重新连接互联网后终止(我有理由这样做,请不要问我原因)并在互联网启动时再次运行。日志看起来像这样:

05-01-2015_09:49:49 Internet terkoneksi ke server OpenVPN
05-01-2015_10:32:45 Openvpn Disconnected. Koneksi internet direstart
---------------------------------------------------------------------------------------------------
05-01-2015_10:33:20 Internet terkoneksi ke server OpenVPN
05-01-2015_11:27:59 Openvpn Disconnected. Koneksi internet direstart
---------------------------------------------------------------------------------------------------
05-01-2015_11:28:46 Internet terkoneksi ke server OpenVPN

我想计算一天中发生了多少次重新连接,我希望日志文件是这样的:

05-01-2015_09:49:49 Internet terkoneksi ke server OpenVPN
05-01-2015_10:32:45 Openvpn Disconnected. Koneksi internet direstart
05-01-2015_10:32:59 Your internet has been reconnected for 6 times today
---------------------------------------------------------------------------------------------------
05-01-2015_10:33:20 Internet terkoneksi ke server OpenVPN
05-01-2015_11:27:59 Openvpn Disconnected. Koneksi internet direstart
05-01-2015_10:32:59 Your internet has been reconnected for 7 times today
---------------------------------------------------------------------------------------------------
05-01-2015_10:33:20 Internet terkoneksi ke server OpenVPN
05-01-2015_11:27:59 Openvpn Disconnected. Koneksi internet direstart
05-01-2015_10:32:59 Your internet has been reconnected for 8 times today

这可能吗?请注意,我的脚本在重新连接调制解调器后终止,并在互联网启动时再次调用,我不希望我的脚本一天24小时运行。

1 个答案:

答案 0 :(得分:0)

保持日志文件清洁更有意义,并创建一个单独的日志分析器脚本来计算每天的连接数。

cut -s -d_ -f1 reconnect.log | uniq -c