[lingyun@localhost rootfs]$ man flock
FLOCK(1) H. Peter Anvin FLOCK(1)
NAME
flock - Manage locks from shell scripts
SYNOPSIS
flock [-sxon] [-w timeout] lockfile [-c] command...
flock [-sxon] [-w timeout] lockdir [-c] command...
flock [-sxun] [-w timeout] fd
DESCRIPTION
This utility manages flock(2) locks from within shell scripts or the command line.
The first and second forms wraps the lock around the executing a command, in a manner similar to su(1) or
newgrp(1). It locks a specified file or directory, which is created (assuming appropriate permissions),
if it does not already exist.
The third form is convenient inside shell scripts, and is usually used the following manner:
(
flock -s 200
# ... commands executed under lock ...
) 200>/var/lock/mylockfile
The mode used to open the file doesn’t matter to flock; using > or >> allows the lockfile to be created
if it does not already exist, however, write permission is required; using < requires that the file
already exists but only read permission is required.
By default, if the lock cannot be immediately acquired, flock waits until the lock is available.
OPTIONS
-s, --shared
Obtain a shared lock, sometimes called a read lock.
-x, -e, --exclusive
Obtain an exclusive lock, sometimes called a write lock. This is the default.
-u, --unlock
Drop a lock. This is usually not required, since a lock is automatically dropped when the file is
closed. However, it may be required in special cases, for example if the enclosed command group
may have forked a background process which should not be holding the lock.
-n, --nb, --nonblock
Fail (with an exit code of 1) rather than wait if the lock cannot be immediately acquired.
-w, --wait, --timeout seconds
Fail (with an exit code of 1) if the lock cannot be acquired within seconds seconds. Decimal
fractional values are allowed.
-o, --close
Close the file descriptor on which the lock is held before executing command. This is useful if
command spawns a child process which should not be hold ing the lock.
-c, --command command
Pass a single command to the shell with -c.
-h, --help
Print a help message.
AUTHOR
Written by H. Peter Anvin <hpa@zytor.com>.
COPYRIGHT
Copyright © 2003-2006 H. Peter Anvin.
This is free software; see the source for copying conditions. There is NO warranty; not even for MER-
CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
SEE ALSO
flock(2)
AVAILABILITY
The flock command is part of the util-linux-ng package and is available from ftp://ftp.ker-
nel.org/pub/linux/utils/util-linux-ng/.
flock utility 4 Feb 2006 FLOCK(1)
(END)
介绍一下参数:
-s为共享锁,在定向为某文件的FD上设置共享锁而未释放锁的时间内,其他进程试图在定向为此文件的FD上设置独占锁的请求失败,而其他进程试图在定向为此文件的FD上设置共享锁的请求会成功。
-e为独占或排他锁,在定向为某文件的FD上设置独占锁而未释放锁的时间内,其他进程试图在定向为此文件的FD上设置共享锁或独占锁都会失败。只要未设置-s参数,此参数默认被设置。
-u手动解锁,一般情况不必须,当FD关闭时,系统会自动解锁,此参数用于脚本命令一部分需要异步执行,一部分可以同步执行的情况。
-n为非阻塞模式,当试图设置锁失败,采用非阻塞模式,直接返回1,并继续执行下面语句。
-w设置阻塞超时,当超过设置的秒数,就跳出阻塞,返回值设置为1,并继续执行下面语句。
-o必须是使用第一种格式时才可用,表示当执行command前关闭设置锁的FD,以使command的子进程不保持锁。
-c执行其后的comand。
举个实用的例子:
[lingyun@localhost rootfs]$ cat mylockfile.sh
#!/bin/shset -x
#exec 6<>"mylockfile.sh"
{
flock -n 6
#[ "$?" -eq "1" ] && {echo "fail";exit;}
if [ "$?" -eq "1" ] ; then
echo "fail";
exit;
fi
echo $$
sleep 10
} 6<>"mylockfile.sh"
[lingyun@localhost rootfs]$
[lingyun@localhost rootfs]$ chmod a+x mylockfile.sh
第一个终端:
[lingyun@localhost rootfs]$ sh mylockfile.sh
+ flock -n 6
+ '[' 0 -eq 1 ']'
+ echo 27292
27292
+ sleep 10
睡眠10秒钟,同时我在另一个终端上运行这个脚本会出现如下情形:
第二个终端:
[lingyun@localhost rootfs]$ sh mylockfile.sh
+ flock -n 6
+ '[' 1 -eq 1 ']'
+ echo fail
fail
+ exit
所以理解-n选项后就会明白了