0
[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

easier to read format:

#!/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

expected output:

[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

actual:

[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

EDIT: Ok so I've got the statistics appending to the log file, but whenever I send the SIGINT symbol with CTRL+\ ping restarts and the statistics are not logged. I'd like to be able to still pause and see the statistics in terminal without restarting ping if possible

[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

script:

#!/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

Add the --foreground option to yout timeout command as ping seems to test that it is being used on a tty to setup signal handling.


Or simply stop using timeout and ask ping to stop after 60*60 seconds:

 ping -w 3600 96.120.16.121 

For your new question, ignore the signals in the rest of your pipe:

#!/bin/bash
ping -w 3600 96.120.16.121 | 
(    trap '' quit
     ts '%Y-%m-%d %H:%M:%S |' | 
     tee -i -a ping.log
)
  • timeout: unrecognized option '--foreground' sorry i mislabeled debian. im on centos – randomdev2 Oct 30 '15 at 12:42
  • 1
    I have timeout --version (GNU coreutils) 8.22. Can't you just use ping -w 3600 and it will stop without needing timeout. – meuh Oct 30 '15 at 12:49
  • strange my timeout --version is 8.4, but my man timeout doesn't have --foreground – randomdev2 Oct 30 '15 at 12:55
  • I agree with meuh, use the -w option in the ping command.. it will avoid any possible signal issue. Also I recommend not using tee. or if you do turn off stdout buffering. Instead use > to redirect to file, and if you must see stuff on screen start a backgroud tail -f process that monitors the log file for you. – Rob Oct 30 '15 at 13:18
  • indeed i got rid of timeout and switched to ping -w. i would like to see stdout, and i would look into tail that monitors the log file but the next step after this is to grep only the statistics lines to the log while still having pings normal stdout in terminal – randomdev2 Oct 30 '15 at 13:25
1

This runs indefinitely, never restarts ping, ping prints stats periodically (adjust the sleep in the following code). Bash is optional this is /bin/sh compatible. You can kill this with CTRL-C or SIGHUP, SIGINT, SIQUIT, or SIGTERM.

#!/bin/sh

trap 'kill -TERM $! 2>/dev/null; exit' HUP INT QUIT TERM

while true; do
        (
            ping localhost 2>&1 &
            trap "kill -INT $!" HUP INT QUIT TERM
            while true
            do
                sleep 5;
                if ! ps $! > /dev/null || ! kill -QUIT $!
                then break
                fi
            done &
            wait
        ) |
        ts '%Y-%m-%d~%H:%M:%S' |
        tee -i ping_$(date +'%Y-%m-%d_%H.%M.%S').log
done
  • this seems to have the same effect as removing timeout and adding the -w flag to ping as suggested by @meuh but when i lengthen the desired time, such as 5 to 3600, ping is restarted whenever i send the SIGINT signal, i.e. CTRL+\. id like to be able to check the statistics without having ping restarted if possible – randomdev2 Oct 30 '15 at 13:02
  • SIGINT and CTRL+\ are different signals. SIGINT is number 2 and key CTLR+C, SIGQUIT is number 3 and key CTRL+\. Type kill -l to list the signals. A good reason to use the mnemonics. – RobertL Oct 30 '15 at 13:24
  • its correctly writing the statistics to the log file with ping -w, so i got rid of timeout. now the problem is ping restarts whenever i CTRL+\ instead of just outputting the statistics and continuing – randomdev2 Oct 30 '15 at 13:32
  • Please post the output of this command: stty | grep intr. What version of unix or linux are you using? – RobertL Oct 30 '15 at 13:36
  • that command doesnt output anything. im on centos 6: heres my uname -a: Linux localhost.localdomain 2.6.32-573.7.1.el6.i686 #1 SMP Tue Sep 22 19:05:58 UTC 2015 i686 i686 i386 GNU/Linux – randomdev2 Oct 30 '15 at 13:38
0

By default, tee command stop writing and closes the file after receive the signal to stop (a Ctlr-C, sent by timeout command).

Just add the option -i to tee and have fun.

Example:

#!/bin/bash
while true; do
        timeout -s 2 1h ping 96.120.16.121 | 
        ts '%Y-%m-%d~%H:%M:%S' |
        tee -i ping_$(date +'%Y-%m-%d_%H.%M.%S').log
done
  • -i doesnt seem to be working. same result as above. i added the -a flag instead of a new log file with a timestamp – randomdev2 Oct 30 '15 at 12:46
  • Interesting, I tested before write the comment. Just tried again and worked as before. Please try the output redirector (>) instead of tee command and let me know. – Willian Paixao Oct 30 '15 at 12:50
  • yes CTRL+\ or sending SIGINT restarts ping, but i definitely think youre on to something and my problem has to do with tee and signals – randomdev2 Oct 30 '15 at 13:07
  • changing | tee -i -a ping.log to >> ping.log yielded the same results yet again. the statistics are only saved if ping ends by itself; if i CTRL+\ then ping restarts and the statistics aren't saved – randomdev2 Oct 30 '15 at 13:18
  • Guess your different environment are giving different results. I'm running Debian 8. – Willian Paixao Oct 30 '15 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.