crontabが奇妙に振る舞う

crontabが奇妙に振る舞う

それでは私の最後の質問に基づいてここ私はうまくいきましたが、2分ごとに特定の日付の行数を確認するcrontabを作成しました。

私のスクリプトは次のとおりです

1 test=/root/test
2 n="$(cat /root/test)"
3 t="$(date)"
4 echo "there were $n lines in $test at $t" >> rtest1

スクリプトを実行するたびに、rtest1は希望の結果を表示します。

there were 224 lines in /root/test at Fri Aug 10 10:28:25 EEST 2018
there were 224 lines in /root/test at Fri Aug 10 10:28:25 EEST 2018
there were 224 lines in /root/test at Fri Aug 10 10:28:26 EEST 2018
there were 224 lines in /root/test at Fri Aug 10 10:28:26 EEST 2018
there were 224 lines in /root/test at Fri Aug 10 10:28:26 EEST 2018
there were 224 lines in /root/test at Fri Aug 10 10:28:26 EEST 2018

しかし、crontabでこれが起こると、奇妙な理由で次のように行番号が省略されることがあります。

there were 229 lines in /root/test at Fri Aug 10 10:20:51 EEST 2018
there were  lines in /root/test at Fri Aug 10 10:22:01 EEST 2018
there were 224 lines in /root/test at Fri Aug 10 10:24:01 EEST 2018
there were 224 lines in /root/test at Fri Aug 10 10:26:02 EEST 2018
there were  lines in /root/test at Fri Aug 10 10:28:01 EEST 2018

私のcrontabは次のようになります。

[root@centos7desk ~]# crontab -l
* * * * * ps axu | wc -l > /root/test
*/2 * * * * /root/script.sh

なぜこれが起こるのかわかりません。

ベストアンサー1

デフォルトでは、2つのcrontabエントリが同時に実行される場合、これは競合状態です。 「値なし」の場合、出力ファイルは生成されますが、まだ入力されていません(ps axu | wc -lランタイムがスクリプトより長くなる可能性があるため)。

この問題を克服するために、sleep 5スクリプトの先頭にを追加できます。 (技術的にこれは競合状態を防ぐことはできませんが、システムの負荷が重大でない限り発生する可能性が低くなります)、またはすべてをスクリプトに入れます(これはより良い解決策かもしれません)。

おすすめ記事