1

I asked for some help with speedtest-cli a few days ago and now I need help with the following:

I made this crontab:

0 * * * * (date +"%A, %d.%m.%Y %X Uhr" >> /home/pi/speedtest/speed.txt; /usr/bin/speedtest-cli --server 3645 --simple >> /home/pi/speedtest/speed.txt; echo "\n" >> /home/pi/speedtest/speed.txt)
10 * * * * (speedtest-cli --server 3645 --share; grep -Po 'Share results: \K.*' >> /home/pi/speedtest/latest.txt)

(look here to see it better formatted: https://hastebin.com/holugehuvo.js)

I'll explain, what I want to do with it: So the first command should print the current date and time in my region format into a text file. The output of speedtest-cli --server 3645 --simple should be appended to the same file and after that should be a new line so that I can see the next output (output one hour later) correctly formatted.

An output of the command speedtest-cli --server 3645 --simple would be something like that:

Ping: xx.xx ms
Download: 53.09 Mbit/s
Upload: 7.92 Mbit/s

This should be repeated every hour.

And the second cron should grep the http:// address returned by the command and print that into a file called latest.txt.

An example output for speedtest-cli --server 3645 --share would be sth like that:

Retrieving speedtest.net configuration...
Testing from Provider (xx.xxx.xxx.xxx)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Citycom Telekommunikation GmbH (Graz) [xx.xx km]: xx.xxx ms
Testing download speed................................................................................
Download: 52.88 Mbit/s
Testing upload speed....................................................................................................
Upload: 7.62 Mbit/s
Share results: http://www.speedtest.net/result/7799311524.png

I just need that http:// address because I want to read that in PHP and display this png on my local web page so that I know what was my last speed test.

Both commands should run once per hour, I used a little offset so that the speed is not influenced by the other speedtest made in the first command.

I need both outputs because I want to monitor my last speeds AND have that image accessible with PHP.

I know that this was/is a long question, but I want to give you many details so that it's easier to understand what I wanted to do.

Thank you for your help.

Michael Kargl
  • 662
  • 10
  • 21
user316860
  • 23
  • 5

1 Answers1

0

There are several points of improvement here:

  1. Usage of %:

    You have to escape it and write it as \%

    The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or a % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. A % character in the command, unless escaped with a backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

    source: man 5 crontab

  2. Compound commands

    You write multiple commands in a single crontab command as

    ( command1; command2 ) >> /path/to/output
    

    While this works, it must be mentioned that this will spawn a sub-shell and is a bit an overkill. You might consider not to do so using:

    { command1; command2; } >> /path/to/output
    

    or if you do not want command2 to execute if command1 failed, you could do:

    command1 && command2 >> /path/to/output
    
  3. Usage of pipe: Your second command should use a pipe (|)

    speedtest-cli --server 3645 --share | grep -Po 'Share results: \K.*' >> /home/pi/speedtest/latest.txt
    
kvantour
  • 25,269
  • 4
  • 47
  • 72