in reply to Make a perl script self aware...

I think the canonical way to do this (in a script not a module) is

use Fcntl ':flock'; # import LOCK_* constants INIT{ open *{0} or die "What!? $0:$!"; flock *{0}, LOCK_EX|LOCK_NB or die "$0 is already running somewhere!\n"; }

It has one disadvantage that on at least one system and on one version of Perl (AS635 on Win2k) you won't see any error message or script generated message as Perl will fail (quietly!?) if you try to run the script while it is already running.


---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

Replies are listed 'Best First'.
Re: Re: Make a perl script self aware...
by waswas-fng (Curate) on Aug 01, 2003 at 19:26 UTC
    This is by far te best way to go, it resolves issues where the system is powered down fast of the application is terminated without its own end block executing to clean up a pid file or lock file. The OS level lock will get cleared if the application is not running or on a reboot. One of the only reasons to use a PDI file (and there are better ways to do this if need be) is to have a way for other applications to actualy start, stop or send interupts etc to a running proccess.

    -Waswas