关闭

20110121

标签: shelldeletefileincludeheaderoutput
333人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:17518次
    • 积分:389
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:2篇
    • 译文:0篇
    • 评论:310条
    最新评论