1

I have installed speedtest.net CLI and written a simple ZSH shell script to run the speedtest binary and generate CSV output.

Want to run this script every [time period] (initially every hour) and collect speedtest data into a log file for later analysis. After reading about cron vs. launchctl on Mac, I decided to create a native launchctl plist Agent for my script and set it to run the script [at teh designated time period].

The script runs successfully when run independently from launchctl; however, when lauchctl runs the script, it does not wait for the completion of the external call to the speedtest binary and jumps straight to the final echo statement. The output file has the time and date successfully appended, but is missing the output from the speedtest binary.

I have tried sleep, wait and a loop to inspect the PID ID (which I believe is found in the $! variable), but the same thing happens. So really need some help to understand what is happening and how to fix it.

PLIST LaunchAgent

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
        <string>com.dazzur.runspeedtest</string>
    <key>StandardOutPath</key>
        <string>/Users/dazzur/OneDrive/dazDocs/Development/work/speedtestresults/speedtestresults.csv</string>
    <key>ProgramArguments</key>
        <array>
            <string>/Users/dazzur/OneDrive/dazDocs/Development/work/speedtestresults/runspeedtest.sh</string>
        </array>
    <key>RunAtLoad</key>
        <true/>
    <key>ThrottleInterval</key>
        <integer>60</integer>
    <key>KeepAlive</key>
        <true/>
    <key>HardResourceLimits</key>
        <dict>
            <key>FileSize</key>
            <integer>10485760</integer>
        </dict>
    <key>SoftResourceLimits</key>
        <dict>
            <key>FileSize</key>
            <integer>5242880</integer>
        </dict>
</dict>
</plist>

ZSH Shell Script

#!/bin/zsh

dtFormatted=$(date "+%F %T")

csvResults=$(speedtest --format=csv)

while ($!)
    do
        sleep 10
done

echo '"'$dtFormatted'"',$csvResults

FYI - I found this site really helpful for understanding how launchctl works - https://www.launchd.info/ and the trial version of the Launch Control tool to check that what I did is working https://www.soma-zone.com/LaunchControl/.

dazzur
  • 11
  • 1

0 Answers0