【问题标题】:Cron not writing output to log fileCron 未将输出写入日志文件
【发布时间】: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 下载速度正常 上传速度正常 延迟在正常阈值内
  • 脚本的第一行必须是SHEBANGHASBANG,而且cronPATH 集非常有限,它可能找不到您的可执行应用程序。在crontab 文件/条目中设置您的路径或使用绝对PATH,。请参阅有关您的 cron 实施的手册。 man 5 crontab
  • 或查看stackoverflow.com/tags/cron/info。祝你好运。
  • @crypto-karski :至少在调试时,我还会将脚本的标准错误重定向到某个文件以供以后调查。

标签: bash macos shell logging cron


【解决方案1】:

我联系了一个 Mac Admin Slack 小组,据他们说,MacOS 已弃用 cron...? 我已经在我的 crontab 和我的 shell 脚本中指定了所有 PATH(谢谢 Jetchisel) ,但遇到了问题。奇怪的是,它在我的 linux 机器上运行得很好,这就是让我伸出援手的原因。

slack 小组告诉我不要执行 cron 作业,而是使用 Launch Daemon 设置调度。这行得通,我对此并不满意,但很高兴让它发挥作用。

感谢大家花时间提供意见!

【讨论】:

    猜你喜欢
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 2012-10-06
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多