Cron not writing output to log file

Viewed 192

I have a speedtest script that I wrote in Bash for MacOS, utilizing Ooklas Speedtest Cli utility, which executes perfectly when manually ran, but when I set my crontab it does not write to the log file in Users/Shared, although it does create the log file. I believe my cron tab is correct. I am using a third party speedtest and gsed binary not native to MacOS, could this be the issue? It almost looks like it bypasses the commands speedtest | gsed -r 's/.* ([0-9]+\.*[0-9]*).*?/\1/' and just creates the file. Does cron do something different then then manually running a script?

Any advice would be GREATLY appreciated, and I apologize for the sloppiness!

#this script runs a speedtest from a server and emails sysadmins if the download, upload, or latency
#reaches a certain threshold
#!/bin/bash

#insturctions
#install home brew; install speedtest cli; install mailutlis if necessary; configure postfix
#install gsed
#brew install gnu-sed

#debugging
#set -x


#network variables
expected_upload=5.00
expected_download=10.00
expected_latency=50.00


today=$(date +"%Y-%m-%d_%H-%M")

echo about to run speedtest

#formatting document
speedtest | gsed -r 's/.* ([0-9]+\.*[0-9]*).*?/\1/' > /Users/Shared/speedtest_logs/$today.txt

echo finished running speedtest 

echo assigning variables from results

#variables are pulled from speedtest
latency=$(awk 'FNR == 6 { print $1}' /Users/Shared/speedtest_logs/$today.txt)
download=$(awk 'FNR == 7 { print $1}' /Users/Shared/speedtest_logs/$today.txt)
upload=$(awk 'FNR == 8 { print $1}' /Users/Shared/speedtest_logs/$today.txt)

echo $HOSTNAME current upload speed is $upload
echo $HOSTNAME current download speed is $download
echo $HOSTNAME latency to the speedtest server is $latency


#determining if your download speed is lower than expected
if (( $(echo "$download < $expected_download" |bc -l) )); then
    echo download speed below expected theshold of 10 mbps - investigate
    #insert email trigger


mail -s "WARNING DEPRICATED DOWNLOAD AT $HOSTNAME" test@gmail.com <<EOF

Current download speed at $HOSTNAME is "$download"mbps

EOF

    # WHEN USING SSMTP echo -e "Subject:Depricated Download Speed\ncurrent download speed currently $download mbps"| /usr/sbin/ssmtp test@gmail.com 
else echo download speeds are normal

fi


#determining if your upload speed is lower than expected
if (( $(echo "$upload < $expected_upload" |bc -l) )); then
    echo upload speed below expected theshold of 5 mbps - investigate
    #insert email trigger

mail -s "WARNING DEPRICATED UPLOAD AT $HOSTNAME" test@gmail.com <<EOF

Current upload speed at $HOSTNAME is "$upload"mbps 

EOF


 #WHEN USING SSMTP echo -e "Subject:Depricated upload Speed\ncurrent upload speed currently $upload mbps"| /usr/sbin/ssmtp test@gmail.com 
else echo upload speeds are normal 

fi

#determining if your latency is within your defined threshold of < 50ms
if  (( $(echo "$latency > $expected_latency" |bc -l) )); then
    echo latency to speedtest server is high - investigate
    #insert email trigger

mail -s "WARNING INCREASED LATENCY AT $HOSTNAME" test@gmail.com <<EOF

Current latency for $HOSTNAME is "$latency"ms

EOF


   #WHEN USING SSMTP echo -e "Subject:High Latency\ncurrent latency $latency ms"| /usr/sbin/ssmtp test@gmail.com 
else echo latency is within the normal threshold

fi
1 Answers

I reached out to a Mac Admin Slack group, and according to them, MacOS deprecated cron...? I had specified all PATHS in my crontab and in my shell script (thank you Jetchisel), but was having issues. Weirdly enough it worked just fine on my linux box, which is what made me reach out.

The slack group told me instead of doing a cron job, to set scheduling with a Launch Daemon. This worked, I am not happy about it, but just glad to have it working.

Thank you, everyone, who took the time to offer input!

Related