LoginSignup
1
0

More than 1 year has passed since last update.

cron で /dev/null つけたら動かない?

Posted at

現象

cron 起動の処理を下記のように出力を抑える設定にしたら、見た目上 動かなくなりました。

* * * * * php /foo/bar/test.php (動く)
↓
* * * * * php /foo/bar/test.php >/dev/null 2>&1 (動かない?)

原因

実際は動いていたのですが、プログラム内の二重起動チェックですぐに終了していたからでした。

二重起動チェックの方法は ps コマンドを発行して、test.php の行数を数えてました。
通常は、

362165 ?  R  0:00 php /foo/bar/test.php

この1行のみ出力されます。これは自分自身であり二重起動していないと判定して処理続行します。
しかし /dev/null をつけると、

362162 ?  Ss  0:00 /bin/sh -c php /foo/bar/test.php >/dev/null 2>&1
362165 ?  R   0:00 php /foo/bar/test.php

この2行が出力されてしまいます。
つまり自分自身を含めて2個の処理が動いていると判定して、終了していたのです。
(実際は1個なのに)

まあ手作りの二重起動チェックが厳密でなかったということでしょう。

その他

例えば

* * * * * php /foo/bar/test.php 1

このように何か引数を追加するだけでは今回の問題は起きません。
>/dev/null 2>&1 を追加すると /bin/sh を介して起動するので、二重起動に引っかかります。

1
0
2

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
1
0