halockrun
NAME
halockrun - Run command while holding exclusive lock on file.SYNOPSIS
halockrun [-ancfv] [-e exitcode] [-E nexitcode] lockfile command [args]halockrun -t|-N [-nc] [-e exitcode] [-E nexitcode] lockfile
DESCRIPTION
Executes command with args while holding an exclusive lock on the file lockfile. If the file is locked by another process, halockrun command then.halockrun is often used to avoid cron job overruns.
halockrun
uses operating system locks (POSIX --
The default value for this option is
exitcode
in non-blocking mode and 1 in testing-, and noop-mode.
This option is useful to make sure that the command does not see any
difference when running under protection of
halockrun.
In other words, the fork mode makes sure that the open file handle to
lockfile
is not visible to
command.
Fork mode has also influence on the
-t
switch, because that will not return the PID of
command
but of the parent
halockrun
process.
This option is new to version V0.99.08, prior this version
halockrun
has always
fork(2)ed.
The default was changed because reliability
is more important than transparency--in that case.
If you are in doubt about this option: don't use it.
Regardless of the lock status,
halockrun
will not run any command.
When the
-t
flags is specified the flags
-a,
-n
and
-f
are meaningless and will be ignored if they are specified.
The
-N
switch is more portable and used to synchronize with a running process. The
-t
switch is used to obtain the PID of the running process.
When the
-N
flag is specified, the
-a
and
-f
flags are meaningless and will be ignored if they are specified.
If
-t
or
-N
was used, 0 is returned if the lock is not currently busy.
On failure--if
lockfile
doesn't exist or
command
can't be executed--halockrun
returns the value specified by the
-e
option.
On failure to obtain the lock in testing mode or in noop mode (
-t
or
-N
) the value specified by the
-E
option is returned or
1
per default.
On failure to obtain the lock in non-blocking mode
halockrun
returns the values specified by the
-E
option.
would be transformed to
This will prevent multiple parallel instances of
/path/script.
Additionally, you might use
hatimerun
to kill
script
when it takes longer then 5 minutes:
You can configure
hatimerun
for many different actions, please refer to the
hatimerun(1)
manpage for the full documentation.
halockrun
uses
fcntl(2)
advisory locking. Similar tools that use a different locking appraoch like
flock(2)
of
lockf(3)
may or may not work together.
For example, locks placed by
flock(2)
on Linux are independend from locks placed by
fcntl(2)
on the same system. That means, if you mix those two methods, they don't lock each other.
halockrun lockfile /bin/true
It supplements the
-t
switch because it blocks. It can be combined with the
-n
option to change the blocking behavior.
However, it doesn't print the PID of the the lock holder if it is busy.
RETURN VALUE
On success, if
command
was executed,
halockrun
returns the return value of
command.
EXAMPLES
The most prominent use of
halockrun
is to protect from cron overrun. A typical 5 minutes cron job like that
*/5 * * * * /path/script
*/5 * * * *
halockrun -cn /path/lock
/path/script
*/5 * * * *
hatimerun -t 5:00
halockrun -cn /path/lock
/path/script
COMPATIBILITY
AUTHOR
Copyright (c) 2001,2003,2005-2010 by Markus Winand <>
SEE ALSO
hatimerun(1)