20110121

1. ln方法(原子的)

#!/bin/sh

LOCKFILE=/tmp/`basename $0`.lock
#get lock
ln -s $0 $LOCKFILE
if [ $? -gt 0 ];then
echo "$0 re-entry or pid file forgot to delete, please check it"
exit 2
fi

#do you work at here


#unlock
rm -f $LOCKFILE



2. lock file(原子的)

#!/bin/sh

LOCKFILE=/tmp/abc.pid

umask=`umask`
#get lock
umask 0222
echo $$ > $LOCKFILE

if [ $? -gt 0 ];then
echo "$0 re-entry or pid file forgot to delete, please check it"
exit 2
fi
umask $umask

#do you work at here

#unlock
rm -f $LOCKFILE

3. pidof or ps(不是原子的)

 

pidof不支持脚本代码程序, 因为命令行前面加解析器了)

 

#!/bin/sh

pids=`/pidof you_binexe_filename`
if [ "X${pids}" != "X${$}" ];then
echo "$0 re-entry, pids is $pids"
exit 2
fi

#do you work at here


4. 复杂的方法(不是原子的)

#return the number of running instance of program
# Notes:
# include your self
# this is shell script,use too many commands in it, too slow
# real 0m0.400s
# user 0m0.115s
# sys 0m0.324s
#% time seconds usecs/call calls errors syscall
#------ ----------- ----------- --------- --------- ----------------
# 55.75 0.031227 416 75 36 wait4
# 27.05 0.015151 213 71 read
# 6.77 0.003793 97 39 clone
# 4.41 0.002470 6 396 rt_sigprocmask
# 1.41 0.000788 9 85 6 close
# 1.41 0.000788 7 111 rt_sigaction
# already take care:
# sh -c envionment in crontab
# sh -c with interaction
# ps/grep/strace/vi/vim/more/less/....
# php/perl/python/java
# easy methold:
# for shell(not atom,but easy)
# if [ `ps aux|grep l1.sh|egrep -v -e 'grep|more|vi|vim|sh -c '|wc -l` -gt 0 ];then echo "already running";fi
# for binary with pidof
# pids=`/pidof you_binexe_filename`; if [ if [ "X${pids}" != "X${$}" ];then echo "already running";exit 2;fi
# for only one instance create lock file with ln
# LOCKFILE=/tmp/`basename $0`.lock; ln -s $0 $LOCKFILE; if [ $? -gt 0 ];then echo "already running";exit 2;fi; rm -f $LOCKFILE
# for only one instance create pid
# LOCKFILE=/tmp/abc.pid; umask=`umask`; umask 0222; echo $$ > $LOCKFILE; if [ $? -gt 0 ];then echo "already running";exit 2;fi; umask $umask; rm -f $LOCKFILE
#
function how_many_running
{
if [ "X$1" = "X" ];then
program_name=$0
else
program_name=$1
fi

dir=`dirname $program_name`
if [ "X$dir" = "X." ];then
dir=`pwd`
fi

name=`basename $program_name`

tmpfile=$(mktemp -q /tmp/$name.XXXXXX)
if [ $? -ne 0 ];then
echo "ERROR:$0 can not create a tempfile with mktemp"
return -1
fi
mypid=$$

#get all possible pid
ps -eo "%p %a"|grep $name|awk '{ print $1; }' > $tmpfile
#crawl all pid
n=0
for pid in `cat $tmpfile`;do
shortname=`ps -p $pid -o "%c"|tail -1 `
longname=`ps -p $pid -o "%a"|tail -1 `

if [ "X$shortname" = "XCOMMAND" ];then
#ps output's header
continue
elif [ "X$shortname" = "Xsh" -o "X$shortname" = "Xbash" -o "X$shortname" = "Xcsh" -o "X$shortname" = "Xksh" -o "X$shortname" = "Xtcsh" -o "X$shortname" ];then
#this is a shell program
if [ `echo "$longname"|grep ' -c '|wc -l ` -gt 0 ];then
#there is another script performed by this one, so igorn this one"
continue
fi

((n=$n+1))
elif [ "X$shortname" = "Xjava" -o "X$shortname" = "Xphp" -o "X$shortname" = "Xperl" -o "X$shortname" = "Xpython" ];then
#this is a script program performed from this shell,and you care this program let me to check how many
((n=$n+1))
elif [ "X$shortname" = "X$name" ];then
#this is a bin executable program
((n=$n+1))
elif [ "X$shortname" = "Xvi" -o "X$shortname" = "Xvim" -o "X$shortname" = "Xcat" -o "X$shortname" = "Xmore" -o "X$shortname" = "Xless" -o "X$shortname" = "Xtail" -o "X$shortname" = "Xgrep" -o "X$shortname" = "Xegrep" -o "X$shortname" = "Xkillall" -o "X$shortname" = "Xps" -o "X$shortname" = "Xstrace" -o "X$shortname" = "Xpidof" ];then
#just for alert
echo "WARNING:someone operation(vi/strace/more/.../killall it?) it"
continue
else
echo "ERROR:don not know how to parse: pid id $pid, /proc/$pid/cmdline:"
cat -v "/proc/$pid/cmdline"
fi
done


rm -f $tmpfile
return $n
}

#use example 1:
#to make 3 instance running
#how_many_running
#running_count=$?
#echo "running_count = $running_count"
#if [ $running_count -gt 3 ];then
# echo "another one running"
# exit 1
#fi
#work code ,like
#sleep 1000000

#use example 2:
#to make 3 instance running
#how_many_running "your_worker_program"
#running_count=$?
#echo "running_count = $running_count"
#if [ $running_count -gt 3 ];then
# echo "another one running"
# exit 1
#fi
#work code ,like
#perform your_worker_program &
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值