8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

プロセスの多重起動をアドバイザリロックで防止する

Last updated at Posted at 2013-03-22

プロセスの多重起動の防止にアドバイザリロックを使えば面倒な後処理を行わなくてもカーネルがよきに計らってくれるのでは? と思って試してみた。

#!/bin/bash

lockfile=/tmp/lockfile 

touch $lockfile

exec 9>$lockfile

flock -w 0 -x 9 || {
  echo "duplicate process"
  exit 1
}

# process...

echo -n "[$$] "

for i in {1..10}; do
  echo -n .
  sleep 1
done

echo
  • touch で lockfile を作る、既にあっても inode は変わらない
  • lockfile をディスクリプタ番号 9 で開く
  • ディスクリプタ番号を指定して排他ロックする
  • ロック失敗したら多重起動
  • プロセスが死ねば lockfile のアドバイザリロックは勝手に解放される

上手く動いているように見えるけど・・・


続編

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?