よく見かける方法は、実行するスクリプトでファイルを生成してそのファイル存在チェックで
判定する方法を見かけるが、シェルが途中でハングアップした場合など起動できなくなる。
シェルコマンドのプロセスIDをチェックする正当な方法にすべきだ。
pgrep の結果でチェックする
#!/bin/bash if [ $$ -ne `pgrep -oxf "/bin/bash $0"` ]; then echo "2重起動エラー" exit 1 fi # TODO
`pgrep -oxf "$0"` が正しいかもしれない。。
引数があるなら、
#!/bin/bash CMDLINE="/bin/bash $0 $@" if [ $$ -ne `pgrep -oxf "$CMDLINE"` ]; then echo "2重起動エラー" exit 1 fi
今更、C-shell なんて書かないだろうけど、C-shell なら、
#!/bin/csh set CMDLINE=`cat /proc/$$/cmdline xargs --null` if ($$ != `pgrep -oxf "$CMDLINE"`) then echo "2重起動エラー" exit 1 endif