
xx_x__x.log
毎週日曜日の午前2時30分に、その場所にファイルが存在するか、ファイルサイズが0であることを確認したいと思います。ファイルサイズを確認してファイルがあるかどうかを確認するスクリプトがありますが、特定の時間に確認する方法がわかりません。
答え1
~からman bash | less '+/^\s*CONDITIONAL'
-s file
ファイルが存在し、サイズが 0 より大きい場合は真です。
事前定義された時間にスクリプトを実行するには、コードを実行可能なsciptに入れてcronjobにロードします。
コマンドをライナーに入れてクロタブにロードできると思いますか? (他の人がこれを正常に実行したかどうかを知りたいです。)
しかし、cronコマンドの問題は
- 私が知っている限り、端末に接続されていません。 {確認できますが
lsof
}したがって、cronコマンドをビルドするときに何が間違っているかについてのデバッグ出力/フィードバックを取得できません。 - cronコマンドをテストする前にしばらく待つ必要があります。
このため、コーダーフレンドリーな環境(端末など)でコードを書くことができるように、コマンドを実行可能なスクリプトに入れてから、正しく機能している場合は、呼び出し先のコマンドをロードすることをお勧めします。スケジュールされたタスク
だからこれがあなたのスクリプトだと教えてください/tmp/checksize.sh
#!/bin/bash
if [[ -s /tmp/xx_x__x.log ]]
then
printf "\n\n%s\n\n\n" true > /tmp/sizelog.log
else
printf "\n\n%s\n\n\n" false > /tmp/sizelog.log
fi
ただ実行してcrontab -e
コマンドを追加してください
5 0 * * * /tmp/checksize.sh
意味は次のとおりです#毎日深夜5分走る
適切な瞬間を捉えることについて
~からman 5 crontab
Commands are executed by cron(8) when the minute, hour, and month of
year fields match the current time, and when at least one of the two
day fields (day of month, or day of week) match the current time (see
``Note'' below). cron(8) examines cron entries once every minute. The
time and date fields are:
field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)
[...]
EXAMPLE CRON FILE
The following lists an example of a user crontab file.
# use /bin/bash to run commands, instead of the default /bin/sh
SHELL=/bin/bash
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"
# Run on every second Saturday of the month
0 4 8-14 * * test $(date +\%u) -eq 6 && echo "2nd Saturday"
答え2
特定の場所にファイルがあることを確認するには、ステートメント-f
でテストを使用できます。if
if [ -f "$FILE" ]; then
(the_velour_fogが言ったように、ファイルが空であっても式はTrueを返します)