FLOCK(1)                                                              FLOCK(1)

       flock - lock a file to synchronize command streams

       flock [-cfn] [-EX|SH|UN|NB] file|fd [cmd]
       flock -h
       flock -V

       Flock  applies  or  removes  an advisory lock on the named file or file
       descriptor.  Advisory locks allow cooperating processes to perform con-
       sistent operations on files or synchronize other atomic operations, see

       When flock succeeds, the exit status from the flock command is the exit
       status  of  its  child.   The  default  cmd is ''/bin/true'' so that by
       default flock succeeds.  Flock exits nonzero when it cannot lock  file,
       or when the specified file (fd) cannot be (isn't) opened.

       It flock has to open the file to get the lock it will not pass the cre-
       ated file descriptor to the child process.  (Said process might  unlock
       the descriptor.)  If the named file is all numeric the -f option may be
       used to force it to be interpreted as a file name.

              Create the lock file if it doesn't exist.

              The given file is a file name, even if consists of all digits.

              Print only a brief help message.

              Do not create the lock file if it doesn't exist.   This  is  the

              Set or reset the LOCK_EX bit in the arguments to flock(2).  This
              is the default if no locking bits are given.

              Set or reset the LOCK_NB bit in the arguments to flock(2).

              Set or reset the LOCK_SH bit in the arguments to flock(2).

              Set or reset the LOCK_UN bit in the arguments to flock(2).  This
              option  is  only  useful  when a file descriptor is given as the
              file on which to operate.

              Show only the standerd version banner.

       The option -EX may be spelled -LOCK_EX, likewise the other locking bits
       may be spelled more verbosely.

       The  locking  bits  may  be inclusive or'ed together with a pipe ('|'),
       which must be quoted from the shell.

       flock -EX 1 echo "something important" >>daemon.log
              Avoid interlaminate writes to the log by locking  the  log  file
              before each write.

       flock -EX 1 sh -c "doit1 ; doit2; doit3"
              Run three sequential tasks under the same lock on stdout

       flock share.c ${VISUAL:-vi} share.c
              To  edit  a  shared source file.  Now days most editors lock the
              file for you, so you don't need this anymore.

       flock ~monitor su monitor
              Substitute user to "monitor" while holding a lock  on  her  home
              directory.   This is a primitive way to keep administrators from
              accessing the same pseudo-user account in parallel.

       alias lockdir='exec 7<. && flock -NB -EX 0 0<&7 2>/dev/null || \
                   echo "${0#-}: $PWD: already locked" && false'
              A ksh(1) alias to lock the current working directory, if  possi-

       alias unlockdir='exec 7<&-'
              Unlock a directory locked by the alias above.

       On  some  not-quite  UNIX  systems  the  flock(2) call is emulated with
       lockf(3) which doesn't allow locking of read-only nodes (like  directo-
       ries).   This  is a real loss to the community, as locks on directories
       make life much safer for some updates.

       KS Braunsdorf
       NonPlayer Character Guild
       install at ksb -snip~me- npcguild.org

       sh(1), true(1), flock(2), open(2), xapply(1l), lockf(3)

                                     LOCAL                            FLOCK(1)