lockfile 中文man页面

系统
lockfile 可以用来创建一个或多个 信号量(semaphore) 文件. 如果lockfile不能创建所有指定的文件(按照指定的顺序),那么它会等待一段 休眠期(sleeptime) (默认为8秒)并从不成功的最后一个文件开始重试.你可以指定失败返回之前 重试(retries) 的次数. 如果 retries 的次数设为-1(默认地,也即 -r-1) lockfile会永远重试下去.

NAME(名称)

lockfile - 条件标志文件创建命令  

SYNOPSIS(总览)

lockfile -sleeptime | -r retries |
      -l locktimeout | -s suspend | -! | -ml | -mu | filename ...  

DESCRIPTION(描述)

lockfile 可以用来创建一个或多个 信号量(semaphore) 文件. 如果lockfile不能创建所有指定的文件(按照指定的顺序),那么它会等待一段 休眠期(sleeptime) (默认为8秒)并从不成功的最后一个文件开始重试.你可以指定失败返回之前 重试(retries) 的次数. 如果 retries 的次数设为-1(默认地,也即 -r-1) lockfile会永远重试下去.

如果在所有文件创建之前 retries 的次数已经满了,lockfile会返回失败的信息并删除所有已经创建的文件.

在shell脚本中使用lockfile作为循环的条件可以很容易地通过使用 -! 标识反向退出的状态来实现.为了防止无限循环,除了lockfile已经存在以外的任何原因的失败都不会反向为成功状态,而是仍然以失败状态返回.

所有标识可以在命令行中的任何地方指定,碰到它们就进行处理.命令行简单地由左至右进行解析.

所有通过lockfile创建的文件都是只读的,因此必须使用 rm -f 来删除.

如果你指定了一个 locktimeout 那么lockfile会在lockfile最后一次修改/创建之后locktimeout秒过后强制性地删除(很可能是一些其它程序意外死掉很长一段时间后造成不能清除残余的lockfiles).Lockfile不受时钟偏差的影响.在lockfile强制被清除之后,延迟的 suspend 秒(默认为16)开始计数,这是为了防止无意的直接删除了任何由另一程序新创建的lockfile(与 procmail(1)) 中的 SUSPEND 比较).  

Mailbox locks(邮箱锁定)

如果系统邮件spool目录有权限允许它,或者如果lockfile正好被setgid了,那么它可以通过使用相应的选项 -ml-mu lock和unlock你的系统邮箱.  

EXAMPLES(示例)

假设你想确定访问文件"important"是串行的,也即,没有其它程序或shell脚本允许访问它.为了简省起见,让我们假定它是一个shell脚本.在这个例子中,你应该像这样解决:

... lockfile important.lock ... access_"important"_to_your_hearts_content ... rm -f important.lock ...

现在如果所有访问"important"的脚本按照这条规则,你就可以确保在`lockfile'和`rm'命令之间至多只有一个脚本在执行.  

ENVIRONMENT(环境变量)

LOGNAME
用作隐含变量以确定调用者的登录名

FILES(相关文件)

/etc/passwd
为了验证和/或纠正调用者的登录名(如果需要的话,同时也是为了找出HOME目录)
/var/spool/mail/$LOGNAME.lock
系统邮箱的lockfile,出现在其中的环境变量不由系统的环境变量决定,而是会通过在/etc/passwd中查找确定.

SEE ALSO(另见)

rm(1), mail(1), binmail(1), sendmail(8), procmail(1)  

#p#

NAME

lockfile - conditional semaphore-file creator  

SYNOPSIS

lockfile -sleeptime | -r retries |
      -l locktimeout | -s suspend | -! | -ml | -mu | filename ...  

DESCRIPTION

lockfile can be used to create one or more semaphore files. If lockfile can't create all the specified files (in the specified order), it waits sleeptime (defaults to 8) seconds and retries the last file that didn't succeed. You can specify the number of retries to do until failure is returned. If the number of retries is -1 (default, i.e., -r-1) lockfile will retry forever.

If the number of retries expires before all files have been created, lockfile returns failure and removes all the files it created up till that point.

Using lockfile as the condition of a loop in a shell script can be done easily by using the -! flag to invert the exit status. To prevent infinite loops, failures for any reason other than the lockfile already existing are not inverted to success but rather are still returned as failures.

All flags can be specified anywhere on the command line, they will be processed when encountered. The command line is simply parsed from left to right.

All files created by lockfile will be read-only, and therefore will have to be removed with rm -f.

If you specify a locktimeout then a lockfile will be removed by force after locktimeout seconds have passed since the lockfile was last modified/created (most likely by some other program that unexpectedly died a long time ago, and hence could not clean up any leftover lockfiles). Lockfile is clock skew immune. After a lockfile has been removed by force, a suspension of suspend seconds (defaults to 16) is taken into account, in order to prevent the inadvertent immediate removal of any newly created lockfile by another program (compare SUSPEND in procmail(1)).  

Mailbox locks

If the permissions on the system mail spool directory allow it, or if lockfile is suitably setgid, it will be able to lock and unlock your system mailbox by using the options -ml and -mu respectively.  

EXAMPLES

Suppose you want to make sure that access to the file "important" is serialised, i.e., no more than one program or shell script should be allowed to access it. For simplicity's sake, let's suppose that it is a shell script. In this case you could solve it like this:

... lockfile important.lock ... access_"important"_to_your_hearts_content ... rm -f important.lock ...

Now if all the scripts that access "important" follow this guideline, you will be assured that at most one script will be executing between the `lockfile' and the `rm' commands.  

ENVIRONMENT

LOGNAME
used as a hint to determine the invoker's loginname

FILES

/etc/passwd
to verify and/or correct the invoker's loginname (and to find out his HOME directory, if needed)
/var/mail/$LOGNAME.lock
lockfile for the system mailbox, the environment variables present in here will not be taken from the environment, but will be determined by looking in /etc/passwd

SEE ALSO

rm(1), mail(1), binmail(1), sendmail(8), procmail(1)  

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-25 15:39:42

fcloseall中文man

2011-08-25 15:00:15

cfgetispeed中文man

2011-08-25 09:29:35

udp中文man

2011-08-19 18:35:50

issue中文man

2011-08-25 17:03:51

pclose中文man

2011-08-25 17:40:25

setvbuf中文man

2011-08-23 14:21:16

poweroff中文man

2011-08-24 15:52:59

intro中文man

2011-08-23 13:40:31

2011-08-25 15:54:08

ferror中文man

2011-08-25 14:40:27

basename中文man

2011-08-25 17:24:54

puts中文man

2011-08-24 18:05:31

SHOW中文man

2011-08-25 18:34:55

ungetc中文man

2011-08-23 10:03:40

useradd中文man

2011-08-16 10:42:30

rmmod中文man
点赞
收藏

51CTO技术栈公众号