TransWikia.com

fping with timestamp logging to file named with date

Ask Ubuntu Asked by disco_rogge on December 30, 2020

Hello.
Using my server to log network DC’s that seem to occur sporadicly during the days.
Looked around and collected code and got this to work, wich i launch via cronjob to get one textfile a day.

The program:

#!/bin/bash

## Get current date ##
_now=$(date +"%d_%m_%Y")

## Appending a current date from a $_now to a filename stored in $_file ##
_file="/path/to/file/pinglogger_$_now.txt"

## fping 24h with timestamp to a file with todays date ##
fping -c 86000 -s google.com 192.168.250.1 | while read pong; do echo "$(date): $pong"; done > "$_file" 2>&1

The problem:
This works, but i do not get the last lines of ping statistics when the program ends. Problem started when i found and added | while read pong; do echo "$(date): $pong"; done to the program to add timestamp on the pings.

The soultion:
After tips and trix, final code is this and it writes stout and sterr to output file: pings local host and google for approx 1 day to a file with todays date.:

#!/bin/bash

## Get current date ##
_now=$(date +"%d_%m_%Y")

## Appending a current date from a $_now to a filename stored in $_file ##
_file="/path to file/pinglogger_$_now.txt"

## fping 24h with timestamp to a file with todays date ##
(fping -c 86000 -s google.com 192.168.250.1 | ts) &> "$_file"

One Answer

Using TS to short it down and putting code in () piping bot sterr and stout to file worked.

(fping -c 86000 -s google.com 192.168.250.1 | ts) &> "$_file"

Correct answer by disco_rogge on December 30, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP