Python speedtest.py failing to connect to Internet when run from cron

I have the following code

#!/usr/bin/python3.8 import subprocess import requests import datetime import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) print("============================================================="); now = datetime.datetime.now() print(now); # speedtest --single --json speedout = subprocess.run(['speedtest', '--single', '--json'], stdout=subprocess.PIPE).stdout.decode('utf-8'); print("speedtest output:"+speedout); now = datetime.datetime.now() print(now); print("============================================================="); 

When I run it manually, it generates the following log

============================================================= 2021-04-26 12:23:14.869085 speedtest output:{"download": 48332978.441761896, "upload": 21216098.81470209, "ping": 43.796, "server": {"url": "", "lat": "-17.7450", "lon": "-48.6250", "name": "Caldas Novas", "country": "Brazil", "cc": "BR", "sponsor": "Hot Caldas Internet", "id": "40200", "host": "hotcaldas.lampinc.com.br:8080", "d": 230.63111881620188, "latency": 43.796}, "timestamp": "2021-04-26T15:23:15.343030Z", "bytes_sent": 26738688, "bytes_received": 60631816, "share": null, "client": {"ip": "179.xxx.xxx.14", "lat": "-1xxx", "lon": "-4xxx", "isp": "Vivo", "isprating": "3.7", "rating": "0", "ispdlavg": "0", "ispulavg": "0", "loggedin": "0", "country": "BR"}} 2021-04-26 12:23:39.011183 ============================================================= 

But when I place it on crontab (under my account) to run hourly:

0 * * * * /myscripts/monitors/speedtest.py >> /mylogs/speedtest.log 2>&1 

it generates the following log

ERROR: Unable to connect to servers to test latency. ============================================================= 2021-04-26 13:00:01.687964 speedtest output: 2021-04-26 13:00:13.539705 ============================================================= 

Note how the error message happens before the first ========

What could be happening?

Update. I added flush=True to all prints. It worked once but is failing again. Now the error msg changed

============================================================= 2021-04-26 16:00:01.943727 ERROR: Unable to connect to servers to test latency. speedtest output: 2021-04-26 16:00:03.616226 ============================================================= 
7

1 Answer

I had the same problem before. I set cron to execute every 15 minutes and every task at xx:00 and xx:30 failed. Maybe speedtest has internal server problem.

So a solution to this problem is to avoid specific time. Please try something like this.
5 * * * * /myscripts/monitors/speedtest.py >> /mylogs/speedtest.log 2>&1

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like