in reply to Running speedtest-cli with Perl and cron

Thanks for introducing me to the Speedtest CLI.

Here;s my contribution - this code returns a comma-separated list that can eventually be graphed by a spreadsheet program.

use strict; use warnings; open my $speedtest,"speedtest-cli --simple |" or die "Cannot open spee +dtest:$!"; my %speed = (DATETIME => '"' . GetDateTime()->{datetime} . '"'); while (<$speedtest>) { next unless m/(\w+): ([\d\.]+)/; $speed{$1} = $2; } close $speedtest; print join(",",map {$speed{$_}} qw|DATETIME Download Upload|),"\n"; sub GetDateTime{ local $_={}; @$_{qw|sec min hour mday mon year wday yday isdst|} =localtime(tim +e); $_->{year}+=1900; $_->{mon}+=1; $_->{yyyymmdd} = sprintf "%04d-%02d-%02d", @$_{qw|year mo +n mday|}; $_->{hhmm} = sprintf "%02d:%02d", @$_{qw|hour mi +n|}; $_->{datetime} = $_->{yyyymmdd} . " " . $_->{hhmm}; return $_ }
Output:
"2014-12-11 22:58",53.79,7.19

        "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Replies are listed 'Best First'.
Re^2: Running speedtest-cli with Perl and cron
by ethered (Initiate) on Dec 12, 2014 at 09:13 UTC
    Cool!

    I like the use of print join and I will add this to my script.

    This is the crontab line I use for appending the results in the same file, I also added an \n to my script to make a new line for each test. I don't see the need for throwing this into a .csv or such, so the notepad will suffice.

    What's the best practice for writing to file? Should the file be written by the script, or is it okay to do this with crontab?

    * */12 * * * /usr/bin/perl /bin/speedtest.pl >> /storage/documents/cron/speedtest/speedtest.txt 2>&1

    You guys are awesome! I've never encountered a forum where people answer this fast, and all the response is helpful. I'm going to be a lot on this website henceforth :-)

      ... best practice for writing to file
      My preference is for the program/script to send output to STDOUT. Externally (as you have done it), this is directed to a user-specified location, which could well be a socket to a different machine, plumbed into a pipe, or dispersed somewhere cloudy or smoggy.

              "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams