在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
经常在 shell 脚本里要阻止其它进程,比如 msmtp 自带的mail queue 脚本,这个脚本的互斥做法是不正确的,下面介绍下发现的三个通过文件达到互斥的正确做法。 1. util-linux 的 flock 这个命令有两种用法: flock LOCKFILE COMMAND ( flock -s 200; COMMAND; ) 200>LOCKFILEflock 需要保持打开锁文件,对于第二种使用方式并不方便,而且 -s 方式指定文件句柄可能冲突。好处是不需要显式的解锁,进程退出后锁必然释放。 2. liblockfile1 的 dotlockfile 号称最灵活可靠的文件锁实现。其等待时间跟 -r 指定的重试次数有关,重试时间为 sum(5, 10, ..., min(5*n, 60), ...).锁文件不需要保持打开, 带来的问题是需要用 trap EXIT 确保进程退出时删除锁文件. 3. procmail 的 lockfile 跟 dotlockfile 类似, 但可以一次性创建多个锁文件. 一是利用普通文件,在脚本启动时检查特定文件是否存在,如果存在,则等待一段时间后继续检查,直到文件不存时创建该文件,在脚本结束时删除文件。为确保脚本在异常退出时文件仍然能被删除,可以借助于trap "cmd" EXIT TERM INT命令。一般这类文件存放在/var/lock/目录下,操作系统在启动时会对该目录做清理。
#!/bin/bash LockFile() { find/dev/shm/* -maxdepth 0 -type l -follow -exec unlink {} \; [ -f /dev/shm/${0##*/}]&&exit ln -s /proc/$$/dev/shm/${0##*/} trap "Exit" 0 1 2 3 15 22 24 } Exit() { unlink /dev/shm/${0##*/}; exit 0; } LockFile # main program # program ...... #Exit /var/lock/subsys目录的作用的说明 flock [-sxun] [-w timeout] fd #!/bin/bash { flock -n 3 [ $? -eq 1 ] && { echo fail; exit; } echo $$ sleep 10 } 3<>mylockfile
【背景介绍】 CU上曾经有几个帖子讨论到一个实际问题,就是如何限制同一时刻只允许一个脚本实例运行。其中本版新老斑竹和其它网友都参加了讨论,但以faintblue兄的帖子对大家启发最大,下面的背景介绍中许多内容都是来自于他。在此感谢faintblue兄,也感谢斑竹和其它朋友! 二、加锁的方法。就是脚本在执行开始先试图得到一个“锁”,得到则继续执行,反之就退出。 最近又有网友问起这个问题,促使我又再次思考。从我以前的一个想法发展了一下,换了一种思路,便有豁然开朗的感觉。不敢藏私,写出来请大家debug。^_^ 基本的想法就是:借鉴多进程编程中临界区的概念,如果各个进程进入我们设立的临界区,只可能一个一个地顺序进入,不就能保证每次只有一个脚本运行了吗?怎样建立这样一种临界区呢?我想到了一种方法,就是用管道,多个进程写到同一个管道,只可能一行一行地进入,相应的,另一端也是一行一行地读出,如此就可以实现并行执行的多个进程进入临界区时的“串行化”。这与faintblue兄以前贴出的append文件的方法也是异曲同工。 #!/bin/sh #name: token.sh #function: serialized token distribution, at anytime, only a cerntern number of token given out #usage: token.sh [number] & #number is set to allow number of scripts to run at same time #if no number is given, default value is 1 if [ -p /tmp/p-aquire ]; then rm -f /tmp/p-aquire fi if mkfifo /tmp/p-aquire; then printf "pipe file /tmp/p-aquire created\n" >>token.log else printf "cannot create pipe file /tmp/p-aquire\n" >>token.log exit 1 fi loop_times_before_check=100 if [ -n "$1" ];then limit=$1 else # default concurrence is 1 limit=1 fi number_of_running=0 counter=0 while :;do #check stale token, which owner is died unexpected if [ "$counter" -eq "$loop_times_before_check" ]; then counter=0 for pid in `cat token_file`;do pgrep $pid if [ $? -ne 0 ]; then #remove lock printf "s/ $pid//\nwq\n"|ed -s token_file number_of_running=`expr $number_of_running - 1` fi done fi counter=`expr $counter + 1` # if [ "$number_of_running" -ge "$limit" ];then # token is all given out. bypass all request until a instance to give one back pid=`sed -n '/stop/ {s/\([0-9]\+\) \+stop/\1/p;q}' /tmp/p-aquire` if [ -n "$pid" ]; then # get a token returned printf "s/ $pid//\nwq\n"|ed -s token_file number_of_running=`expr $number_of_running - 1` continue fi else # there is still some token to give out. serve another request read pid action < /tmp/p-aquire if [ "$action" = stop ]; then # one token is given back. printf "s/ $pid//\nwq\n"|ed -s token_file number_of_running=`expr $number_of_running - 1` else # it's a request, give off a token to instance identified by $pid printf " $pid" >> token_file number_of_running=`expr $number_of_running + 1` fi fi done -------------------------------------------------------------------------------------------- 脚本2:并发执行的脚本 -- my-script。在"your code goes here"一行后插入你自己的代码,现有的是我用来测试的。 #!/bin/sh # second to wait that the ditributer gives off a token a_while=1 if [ ! -p /tmp/p-aquire ]; then printf "cannot find file /tmp/p-aquire\n" >&2 exit 1 fi # try to aquire a token printf "$$\n" >> /tmp/p-aquire sleep $a_while # see if we get one grep "$$" token_file if [ $? -ne 0 ]; then # bad luck. :( printf "no token free now, exitting...\n" >&2 exit 2 fi 这个脚本是将文件锁得,不过我对这脚本还有一些疑惑的地方,暂且不讨论,等以后回头再来谈 #!/bin/sh # filelock - A flexible file locking mechanism. retries="10" # default number of retries action="lock" # default action nullcmd="/bin/true" # null command for lockfile while getopts "lur:" opt; do case $opt in l ) action="lock" ;; u ) action="unlock" ;; r ) retries="$OPTARG" ;; esac done shift $(($OPTIND - 1)) if [ $# -eq 0 ] ; then cat << EOF >&2 Usage: $0 [-l|-u] [-r retries] lockfilename Where -l requests a lock (the default), -u requests an unlock, -r X specifies a maximum number of retries before it fails (default = $retries). EOF exit 1 fi # Ascertain whether we have lockf or lockfile system apps if [ -z "$(which lockfile | grep -v '^no ')" ] ; then echo "$0 failed: 'lockfile' utility not found in PATH." >&2 exit 1 fi if [ "$action" = "lock" ] ; then if ! lockfile -1 -r $retries "$1" 2> /dev/null; then echo "$0: Failed: Couldn't create lockfile in time" >&2 exit 1 fi else # action = unlock if [ ! -f "$1" ] ; then echo "$0: Warning: lockfile $1 doesn't exist to unlock" >&2 exit 1 fi rm -f "$1" fi exit 0 |
请发表评论