LoginSignup
18
17

More than 5 years have passed since last update.

testコマンドとcronを組み合わせ、月末にバッチを動かす

Last updated at Posted at 2013-06-06
 0 0 * * * /usr/bin/test $( date -d '+1 day' +\%d ) -eq 1 && /home/hoge/cron/foo.sh

↑で、毎月月末日の零時にfoo.shが実行されるようになります。

testコマンド

条件式を評価して、真(0)偽(1)を終了ステータスとして返す

 date -d '+1 day' +\%d のコマンドの実行結果(翌日の日付)と1 を比較する

例えば、今日は7日なので…$?を出力して条件式の評価をみてみる

 test  $( date -d '+1 day' +\%d ) -eq 1;  echo $?;
=>1

明日は1日ではないので、偽

 test  $( date -d '+1 day' +\%d ) -eq 8;  echo $?;
=>0

明日は8日でなので、真

&&

一つめのコマンドが正常終了した場合に2つめのコマンドを実行する

testコマンドのステータスが真(0)になる場合のみ、/home/hoge/cron/foo.sh が実行される



testコマンドに渡す条件式を工夫すれば、もっと色々できそうですね

18
17
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
17