2021年5月7日星期五

Bash How to kill wget process after a given timeout?

I have a list of wget of file to download and want to see the download speed. I just want to run about 10 seconds, then print out the result what is the download speed. I have 20 different server file to test out. My goal is to see the how kb/s download for that 10 seconds.

e.g

> wget https://lg-lis.fdcservers.net/100MBtest.zip                                                            --2021-05-08 13:37:37--  https://lg-lis.fdcservers.net/100MBtest.zip  Resolving lg-lis.fdcservers.net (lg-lis.fdcservers.net)... 50.7.43.4  Connecting to lg-lis.fdcservers.net (lg-lis.fdcservers.net)|50.7.43.4|:443... connected.  HTTP request sent, awaiting response... 200 OK  Length: 104857600 (100M) [application/zip]  Saving to: '100MBtest.zip'    100MBtest.zip                      0%[                                                         ] 679.66K   174KB/s    eta 9m 26s ^C  

This is my bash file

#!/bin/bash    function speedtest() {        local key=$1      local url=$2         ( cmdpid=$$;        (sleep 10; kill $cmdpid; rm -f 100M) \          & while ! wget "$url"        do             echo -e "\033[40;32;1m$key for 10 seconds done.\033[0m"        done )       }  speedtest "Lisbon" "https://lg-lis.fdcservers.net/100MBtest.zip"  speedtest "London" "https://lg-lon.fdcservers.net/100MBtest.zip"  speedtest "Madrid" "https://lg-mad.fdcservers.net/100MBtest.zip"  speedtest "Paris" "https://lg-par2.fdcservers.net/100MBtest.zip"  

However, the above code does not work, it still download at background and redirect to wget-log

> ./wget_speedtest.sh                                                                                         --2021-05-08 13:41:56--  https://lg-lis.fdcservers.net/100MBtest.zip  Resolving lg-lis.fdcservers.net (lg-lis.fdcservers.net)... 50.7.43.4  Connecting to lg-lis.fdcservers.net (lg-lis.fdcservers.net)|50.7.43.4|:443... connected.  HTTP request sent, awaiting response... 200 OK  Length: 104857600 (100M) [application/octet-stream]  Saving to: '100M'    100M                             5%[==>                                                      ]   5.84M  1.02MB/s    eta 79s    [1]    21251 terminated  ./wget_speedtest.sh    Redirecting output to 'wget-log'.  
https://stackoverflow.com/questions/67444450/bash-how-to-kill-wget-process-after-a-given-timeout May 08, 2021 at 01:44PM

没有评论:

发表评论