mr-canoehead / network_performance_monitor

Network Performance Monitor - a portable tool for troubleshooting performance issues with home networks
GNU General Public License v3.0
84 stars 21 forks source link

Speedtest fails under cron but works manually #25

Closed dutts closed 3 years ago

dutts commented 4 years ago

Hi,

Thank you for doing this, since working from home full time recently and battling with a ropey Sky Broadband connection this has been an invaluable use of a spare pi I had lying around.

I have one issue that I can't figure out though; the Speedtest runs fine when I log in as pi and execute python3 /opt/netperf/test_network.py isp the script completes and my speed test data ends up in the database and graph but never works under cron with this line 5,35 * * * * python3 /opt/netperf/test_network.py isp > /dev/null

I get a zero result and the following entries in netperf.log

2020-11-06 16:05:02,196 test_network INFO:Testing Internet speed...
2020-11-06 16:05:02,204 test_network INFO:Speedtest failed.
2020-11-06 16:05:02,205 netperf_db DEBUG:{'type': 'speedtest', 'data': {'client_id': '60765', 'timestamp': 1604678702.2052596, 'rx_Mbps': 0, 'tx_Mbps': 0, 'rx_bytes': 0, 'tx_bytes': 0, 'remote_host': 'n/a', 'url': 'n/a', 'ping': 0}}
2020-11-06 16:05:02,206 netperf_db DEBUG:message type is: speedtest
2020-11-06 16:05:02,206 netperf_db DEBUG:received message type: speedtest data: {"client_id": "60765", "timestamp": 1604678702.2052596, "rx_Mbps": 0, "tx_Mbps": 0, "rx_bytes": 0, "tx_bytes": 0, "remote_host": "n/a", "url": "n/a", "ping": 0}

Cron's log entry in /var/log/syslog shows it running as user pi

Nov  6 16:05:01 raspberrypi CRON[17221]: (pi) CMD (python3 /opt/netperf/test_network.py isp > /dev/null)

The other cron jobs work fine, any ideas what's going wrong or how I can further diagnose the issue?

Thanks again

Rich

mr-canoehead commented 3 years ago

Does the db row count for the 'speedtest' table increment when the job runs via the 'pi' user's crontab? You can view the rowcounts by using the following command:

/opt/netperf/tools/db_rowcounts.sh

Do any relevant message show up in the syslog, particularly immediately after the cron job runs?

Are you using the open source 'speedtest-cli' or Ookla's client?

Can you please send me the following via email (mr_canoehead@telus.net): 1) /opt/netperf/config/netperf.json 2) the contents of the 'pi' user crontab (crontab -l > crontab.txt)

Just to make sure this isn't a general problem I reinstalled the project on my RPi 4B today (it's been busy with another project), am not experiencing this issue.

Thanks, Chris

dutts commented 3 years ago

Apologies for the delay in getting back to you, I realised that my pi might not have been in such good health as some deeper digging into some other bizarre behaviour was giving me segmentation faults. Have dug out a fresh micros card and will try a new install shortly.

Will close if I can't reproduce.

scradster commented 3 years ago

First of all, thanks a lot @mr-canoehead for the awesome project. Installed it today and it works like a charm! Thanks to the wiki the installation is really straight forward and easy 👍

I faced the same problem as @dutts and was able to resolve it after looking at this: https://stackoverflow.com/questions/14612444/bash-script-runs-manually-but-fails-on-crontab

I changed the following line https://github.com/mr-canoehead/network_performance_monitor/blob/ca88f4010d5171ffea77578a5c6ea350491b0c02/netperf/test_network.py#L145 to include the full path to speedtest-cli which i found using which speedtest-cli like this:

cmd = "{}/usr/local/bin/speedtest-cli --json {}".format(cmd_prefix,speedtest_server_opt)

This resolves the issue for me.

dutts commented 3 years ago

First of all, thanks a lot @mr-canoehead for the awesome project. Installed it today and it works like a charm! Thanks to the wiki the installation is really straight forward and easy 👍

I faced the same problem as @dutts and was able to resolve it after looking at this: https://stackoverflow.com/questions/14612444/bash-script-runs-manually-but-fails-on-crontab

I changed the following line

https://github.com/mr-canoehead/network_performance_monitor/blob/ca88f4010d5171ffea77578a5c6ea350491b0c02/netperf/test_network.py#L145

to include the full path to speedtest-cli which i found using which speedtest-cli like this: cmd = "{}/usr/local/bin/speedtest-cli --json {}".format(cmd_prefix,speedtest_server_opt)

This resolves the issue for me.

Great spot! I hadn’t had a chance to dig into this yet so glad it’s been solved!