Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 13 Apr 2011 11:08:41 +0200
From:      Bastien Semene <bsemene@cyanide-studio.com>
To:        freebsd-questions@freebsd.org
Subject:   lockf command
Message-ID:  <4DA56819.90503@cyanide-studio.com>

next in thread | raw e-mail | index | archive | help
Hi,

I'm writing a (very simple) script to avoid concurrent processes to do 
the same task.

Background:
After a task is done by a service, it synchronizes it with other(s) 
server(s).
I wish that if a synchronization is not done during one or more tasks 
are completed, not concurrent synchronization processes are launched.


According to the flock manual It seems to be simple:
"If lockf successfully acquires the lock, it returns the exit status 
produced by command.
Otherwise, it returns one of the exit codes defined in sysexits(3), as 
follows:"


So this script should work:
"#!/bin/sh

flock -k /tmp/lockfile command arguments"


But as far as I tested it doesn't act like this :
- Command #1 : flock -k /tmp/lockfile read input, the shell is waiting 
for an input.
- Command #2 : flock -k /tmp/lockfile ls -hal /tmp

Launching command #1, then #2.
The command #2 is waiting. When command #1 is finished command #2 is 
executed.

I wish that if command #2 can't acquire the lock, lockf exits (exit 0 
would be nice).
If I set -t 1, lockf is quite what I'm waiting for. But I like to do 
this in a clear way : if it can't acquire the lock it exits, no timeout 
wait.

Am I misunderstanding something ? What should I change ?

Thanks for your help

-- 
Bastien Semene
Administrateur Réseau&  Système

Cyanide Studio - FRANCE




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4DA56819.90503>