【发布时间】:2020-07-13 20:43:10
【问题描述】:
我有一个速度测试脚本,我在 Bash for MacOS 中使用 Ooklas Speedtest Cli 实用程序编写,它在手动运行时完美执行,但是当我设置我的 crontab 时,它不会写入用户/共享中的日志文件,尽管它确实创建了日志文件。我相信我的 cron 选项卡是正确的。我正在使用非 MacOS 原生的第三方 speedtest 和 gsed 二进制文件,这可能是问题吗?看起来它几乎绕过了命令speedtest | gsed -r 's/.* ([0-9]+\.*[0-9]*).*?/\1/' 并只是创建了文件。 cron 会做一些不同于手动运行脚本的事情吗?
任何建议都会非常感谢,我为马虎道歉!
#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
【问题讨论】:
-
我的输出应该是这样的:即将运行 speedtest 完成运行 speedtest 从结果中分配变量
当前上传速度是 316.0 当前下载速度是 63.5 speedtest 服务器的延迟是6.08 下载速度正常 上传速度正常 延迟在正常阈值内 -
脚本的第一行必须是
SHEBANG或HASBANG,而且cron的PATH集非常有限,它可能找不到您的可执行应用程序。在crontab文件/条目中设置您的路径或使用绝对PATH,。请参阅有关您的 cron 实施的手册。man 5 crontab -
或查看stackoverflow.com/tags/cron/info。祝你好运。
-
@crypto-karski :至少在调试时,我还会将脚本的标准错误重定向到某个文件以供以后调查。
标签: bash macos shell logging cron