0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pidofでPHPバッチ処理の二重起動を防止するときの注意点

Posted at

PHPのバッチ処理が二重に起動しないように、「pidof 」というコマンドを利用しました。

こちらの記事を参考にさせていただきました。
[一番簡単かも]cron起動プロセスの多重起動防止ワンライナー

PHPバッチ処理が二重起動してしまった

実際に書いたcronですが、これ、うまくいきませんでした。。

cron
* * * * * /usr/sbin/pidof -x batch.php >/dev/null || /usr/bin/php /path/to/batch.php

/usr/sbin/pidof -x batch.phpを「batch.php」が起動している状態で実行しても、プロセスIDが返ってきません。

自分なりの解釈ですが、起動しているプロセスはあくまで「/usr/bin/php」なので、「batch.php」のプロセスを探しても見つからないのだと思います。

解決策

こちらの記事を参考にさせていただきました。
cron 実行でスクリプトの多重起動を防止する方法

シェルスクリプト経由でPHPバッチ処理を実行したところ、うまくきました!

batch.sh
#!/usr/bin/sh

cd /path/to

/usr/bin/php batch.php
cron
* * * * * /usr/sbin/pidof -x batch.sh >/dev/null || /path/to/batch.sh

シェルスクリプトを実行すると、シェルスクリプト自体が起動プロセスとなるので「pidof」でプロセスIDが返ってくるのだと思います。

0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?