一、比较简单,秒级别可以做到单实例。
is_process_running()
{ (
RUNBIN=
1
R
U
N
B
I
N
B
A
S
E
N
A
M
E
=
1 RUNBIN_BASENAME=
1RUNBINBASENAME=(basename "
R
U
N
B
I
N
"
)
R
U
N
B
I
N
P
I
D
F
I
L
E
=
RUNBIN") RUNBIN_PIDFILE=
RUNBIN")RUNBINPIDFILE=RUN_DIR/$RUNBIN_BASENAME.pid
RUNBIN_PID=getf_pid $RUNBIN_PIDFILE
if [ -z $RUNBIN_PID ]
then
return 0
else
[ ! -z `ps hocmd $RUNBIN_PID` ] && return 1
return 0
fi
) }
二、秒一下,并发启动脚本概率大的请假使用flock方式
利用flock方法在var/lock目录下创建一个以本文件名同名的文件锁
其他同名脚本进来,判断文件锁存在,就会获取失败
用法同is_singleprocess,完全兼容
is_singleprocess_with_flock()
{ (
if [ $# == 1 ]; then
RUNBIN=
1
R
U
N
B
I
N
B
A
S
E
N
A
M
E
=
1 RUNBIN_BASENAME=
1RUNBINBASENAME=(basename "
R
U
N
B
I
N
"
)
R
U
N
B
I
N
P
I
D
F
I
L
E
=
RUNBIN") RUNBIN_PIDFILE=
RUNBIN")RUNBINPIDFILE=RUN_DIR/$RUNBIN_BASENAME.pid
RUNBIN_PID=getf_pid $RUNBIN_PIDFILE
RUNBIN_LOCKFILE=“
L
O
C
K
D
I
R
/
LOCK_DIR/
LOCKDIR/RUNBIN_BASENAME.lock”
elif [ $# == 2 ]; then
RUNBIN=
1
R
U
N
B
I
N
B
A
S
E
N
A
M
E
=
1 RUNBIN_BASENAME=
1RUNBINBASENAME=(basename "
R
U
N
B
I
N
"
)
R
U
N
B
I
N
P
I
D
F
I
L
E
=
RUNBIN") RUNBIN_PIDFILE=
RUNBIN")RUNBINPIDFILE=RUN_DIR/${RUNBIN_BASENAME}$2.pid
RUNBIN_PID=getf_pid $RUNBIN_PIDFILE
RUNBIN_LOCKFILE="
L
O
C
K
D
I
R
/
LOCK_DIR/
LOCKDIR/{RUNBIN_BASENAME}
2.
l
o
c
k
"
e
l
s
e
e
c
h
o
"
p
a
r
a
m
c
o
u
n
t
e
r
r
o
r
,
e
x
i
t
.
.
.
"
r
e
t
u
r
n
1
f
i
L
P
I
D
=
2.lock" else echo "param count error, exit..." return 1 fi LPID=
2.lock"elseecho"paramcounterror,exit..."return1fiLPID=$
{
flock -nx 200
[ $? -ne 0 ] && {
return 1;
}
if [ -z $RUNBIN_PID ]
then
echo $LPID >$RUNBIN_PIDFILE
return 0
else
if [ $LPID == $RUNBIN_PID ]
then
return 0
else
[ ! -z `ps hocmd $RUNBIN_PID | awk '$2=="'$RUNBIN'" {print "RUN"; exit 0; }'` ] && return 1
echo $LPID >$RUNBIN_PIDFILE
return 0
fi
fi
} 200>"$RUNBIN_LOCKFILE"
) }