LoginSignup
1
0

More than 5 years have passed since last update.

時間のかかるMakefileで重複処理を防止する

Last updated at Posted at 2017-10-16

Makefile 便利ですよね
僕はcronと併せて使っています

こんな感じです

crontab
*/5 * * * * make sync
5 * * * * make clean
Makefile

sync:
    rsync -auz /opt/data remotesite:/opt/remote-data/

clean:
    find /opt/data -mtime +3 -type f -exec rm {} \;

でもひとつ困ったことがあって
rsync に予想以上の時間がかかったりすると
処理が終わらないうちに次のサイクルでcron処理がスタートしたりして
いつのまにかプロセスが以上に大量発生してしまう事なんです。
そうすると負荷が異常に大きくなってログインできなくなったりフリーズしてしまったりします。

なので make sync が実行された時に重複して実行されるのを防止したいわけです。

試行錯誤の末 今は次のような方法に落ち着いています

Makefile

sync:
    kill -0 `tail -1 sync.pid` || $(MAKE) sync_do

sync_do:
    ps -p $$$$ -o ppid > sync.pid
    rsync -auz /opt/data remotesite:/opt/remote-data/


clean:
    find /opt/data -mtime +3 -type f -exec rm {} \;

make sync_do を実行するときのPID が sync.pid に保存されるので そのプロセスが生きているかどうかで重複の判定を行っています

似たような状況は色々あって重複実行を防ぎたいケースってちょくちょくあります
厳密なやり方もあるのでしょうが、簡単な方法が欲しい場合もありますよね?
Makefile 以外にも bash スクリプトでもできます

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