Bash変数は、cronのスケジュールの頻度に応じて空です。 1、5、15、または10分ごとに値がある場合。 30時間ごとまたは毎時消去

Bash変数は、cronのスケジュールの頻度に応じて空です。 1、5、15、または10分ごとに値がある場合。 30時間ごとまたは毎時消去

奇妙な動作をするcronで予約されたbashスクリプトがあります。タイトルが示すように、スクリプトが実行された後、クローンスケジュールに応じて空または空の変数があります。* * * * *または、毎分実行すると*/1 * * * *変数がいっぱいになり、スクリプトが機能します。 5分、10分、15分ごとに同じです。 asを使用して30分に移動するか、*/30 * * * *asを使用して1時間ごとに上に移動すると、0 * * * *変数は空です。

スクリプト自体は、変数の値を同じファイルに追加する前に、「日付」の出力をファイルに追加する以外に、何らかの方法で時間を使用しません。

スクリプトは手動で実行すると期待どおりに動作します。 1時間または30分ごとに実行したいが、cronでこれらの変更を実行すると、空の変数が原因でスクリプトが失敗します。

クローンの頻度がこのようにスクリプトにどのような影響を与えるかわかりません。スクリプトは静的です。 cronエントリのみが変更されました。アイデアはありますか?よろしくお願いします。

スクリプト:

#!/bin/bash
# declare variables
resultsdir="/share/CACHEDEV1_DATA/Ian/Documents/IT/Speedtest"
dl="00.00"
#run the download speedtest and grep the Mbit/s result into $dl
dl=$(speedtest --no-upload | grep bit)
#if the $dl variable is empty notify and exit
if [ -z "$dl" ]
then
      /sbin/notice_log_tool -a "Speedtest failed due to empty dl variable" --severity=6
      exit
else
      :
fi
#append the date and time to speedtest.txt
date>>$resultsdir/speedtest.txt
#append the download speed after the date to speedtest.txt
echo $dl>>$resultsdir/speedtest.txt
#append the speedtest Mbit/s numerics to speedtest_numerics.txt
echo $dl | tr -d ' aA-zZ:/'>>$resultsdir/speedtest_numerics.txt
#notify of script completion
/sbin/notice_log_tool -a "Speedtest completed - ${dl}." --severity=6
exit

ワーククローン項目の例:

* * * * * /share/CACHEDEV1_DATA/TFTP/Scripts/speedtest.sh >/dev/null 2>&1

失敗したcron入力の例:

*/30 * * * * /share/CACHEDEV1_DATA/TFTP/Scripts/speedtest.sh >/dev/null 2>&1

$dl成功した実行中のgrepのvar一致の例:

Retrieving speedtest.net configuration... 
Testing from ISP (IP)... 
Retrieving speedtest.net server list... 
Selecting best server based on ping... 
Hosted by ISP [response]  
Testing download speed...
Download: X Mbit/s

$dlジョブの実行時に0 * * * *varには何が含まれますか*/30 * * * *

Retrieving speedtest.net configuration... 
Testing from ISP (IP)...
Retrieving speedtest.net server list...
Selecting best server based on ping... 

speedtest私が定義した完了によると、スクリプトは完了するまで続きます。たぶん、ある種のタイムアウトがあるかもしれません。 1 日に 1 時間または 30 分単位でジョブが 10 個程度しか実行されないため、ロードの問題ではないようです。パフォーマンス指標がよさそうです。このボックスの主な使命はNASおよびPlexメディアサーバーとして機能することであるため、1日の95%はアイドル状態と言えます。スケジュールされたクラウドバックアップなどはありません。

1分ごとに実行するようにスケジュールしましたが、1時間30分を除いて正常に実行されます。

別の方法でスケジュールするか、speedtestダウンロード指標を取得するまで繰り返し実行できますが、これは原因を特定できなかった回避策です。

ベストアンサー1

おすすめ記事