[CentOS] Any one have a good example...

James Olin Oden james.oden at gmail.com
Tue Aug 29 18:48:17 UTC 2006


Actually if you use a language that supports flock() do the following:

   Create lock file at installation of the script.
   In script call flock(2) in a non-blocking manner against this file.
   If you don't aquire the lock then exit with suitable message.
   If you do get the lock do your stuff and exit.

Locks aquired by flock automatically go away when the file handle is
closed (and the filehandle automatically gets closed like or not after
you exit a proccess...also in various languages scoping will also
apply).

This is easily done in perl (man perlfunc and look up flock()), and if
your comfortable in C its pretty trivial create said wrapper.

The key is the file must pre-exist before your script is ever called,
because there is a race condition on creating the file, but you
guarantee the files pre-existance the race condition is removed.
Typically I would make the "lock" file owned by the package that
delivers the software.  No fuss, no muss.

Good luck...james

On 8/29/06, Scott Silva <ssilva at sgvwater.com> wrote:
> ...of a shell script for rsync that won't start again if it is already running?
> I thought of using a lock file, but what if it is killed mid script or bombs?
>
> --
>
> MailScanner is like deodorant...
> You hope everybody uses it, and
> you notice quickly if they don't!!!!
>
> _______________________________________________
> CentOS mailing list
> CentOS at centos.org
> http://lists.centos.org/mailman/listinfo/centos
>



More information about the CentOS mailing list