#!/bin/bash # 2重起動チェック function checkDuplicate() {local RET=0local base=${0##*/}local pidfile="/tmp/${base}.pid"while true; doif ln -s $$ ${pidfile} 2> /dev/nullthen# 起動OKRET=0 && breakelsep=$(ls -l ${pidfile} | sed 's@.* @@g')if [ -z "${p//[0-9]/}" -a -d "/proc/$p" ]; thenlocal mypid=""for mypid in $(pgrep -f ${base})do[ ${p} -eq ${mypid} ] && RET=1 && breakdone;fi[ ${RET} -ne 0 ] && breakfirm -f ${pidfile}done# チェック結果を判定して、pidファイルを作成する。# 正常終了、シグナルを検知するとpidファイルを削除する。if [ ${RET} -eq 0 ]; thentrap "rm -f ${pidfile}; exit 0" EXITtrap "rm -f ${pidfile}; exit 1" 1 2 3 15fireturn ${RET} }
用法
2重起動チェック checkDuplicate if [ 0 -ne $? ]; then# ここにエラー処理を書く fi--------------------------------------------------------------------------------
转载自:http://qiita.com/KurokoSin/items/0eddf05818b89b627102