ping は統計をファイルに出力します。

ping は統計をファイルに出力します。
[root@localhost ~]# while true; do timeout -s 2 1h ping 96.120.16.121 | ts '%Y-%m-%d %H:%M:%S |' | tee -a ping.log; done

より読みやすいフォーマット:

#!/bin/bash
while true; do
        timeout -s 2 1h ping 96.120.16.121 | 
        ts '%Y-%m-%d %H:%M:%S |' |
        tee -a ping.log
done

予想出力:

[root@localhost ~]# cat ping.log
2015-10-30 07:35:29 | PING 96.120.16.121 (96.120.16.121) 56(84) bytes of data.
2015-10-30 07:35:29 | 64 bytes from 96.120.16.121: icmp_seq=1 ttl=254 time=11.2 ms
2015-10-30 07:35:30 | 64 bytes from 96.120.16.121: icmp_seq=2 ttl=254 time=9.83 ms
2015-10-30 07:35:31 | 64 bytes from 96.120.16.121: icmp_seq=3 ttl=254 time=13.0 ms
2015-10-30 07:35:32 | 64 bytes from 96.120.16.121: icmp_seq=4 ttl=254 time=17.0 ms
2015-10-30 07:35:33 | 64 bytes from 96.120.16.121: icmp_seq=5 ttl=254 time=20.8 ms
5/5 packets, 0% loss, min/avg/ewma/max = 9.839/14.399/13.129/20.850 ms
Quit

実際:

[root@localhost ~]# cat ping.log
2015-10-30 07:35:29 | PING 96.120.16.121 (96.120.16.121) 56(84) bytes of data.
2015-10-30 07:35:29 | 64 bytes from 96.120.16.121: icmp_seq=1 ttl=254 time=11.2 ms
2015-10-30 07:35:30 | 64 bytes from 96.120.16.121: icmp_seq=2 ttl=254 time=9.83 ms
2015-10-30 07:35:31 | 64 bytes from 96.120.16.121: icmp_seq=3 ttl=254 time=13.0 ms
2015-10-30 07:35:32 | 64 bytes from 96.120.16.121: icmp_seq=4 ttl=254 time=17.0 ms
2015-10-30 07:35:33 | 64 bytes from 96.120.16.121: icmp_seq=5 ttl=254 time=20.8 ms

編集する:わかりました。ログファイルに統計を追加しましたが、SIGINTCtrl + \を使用してシンボルを送信するたびにpingが再開され、統計は記録されません。可能であれば、pingを再開せずに端末で統計を一時停止して表示できることを願っています。

[root@localhost ~]# while true; do ping -w 3600 96.120.16.121 | ts '%Y-%m-%d %H:%M:%S |' | tee -i -a ping.log; done

スクリプト:

#!/bin/bash
while true; do 
        ping -w 3600 96.120.16.121 | 
        ts '%Y-%m-%d %H:%M:%S |' | 
        tee -i -a ping.log
done

ベストアンサー1

pingが信号処理を設定するためにttyで使用されているかどうかをテストしているように見えるので、--foregroundyoutコマンドにオプションを追加してください。timeout


または、単に使用を中止し、timeout60 * 60秒後にpingを停止するように依頼してください。

 ping -w 3600 96.120.16.121 

新しい質問については、残りのパイプラインの信号を無視してください。

#!/bin/bash
ping -w 3600 96.120.16.121 | 
(    trap '' quit
     ts '%Y-%m-%d %H:%M:%S |' | 
     tee -i -a ping.log
)

おすすめ記事