in reply to Locking a script

i would definitely go for the 'traditional' unix method of creating a lock file somewhere local and then installing an exit handler to delete the lockfile before death.

in pseudo-perl, this would be:

if ( lockfile_exists() ) { warn script_is_locked(); exit 1; } $SIG{'__DIE__'} = \&remove_lockfile(); lock_file(); do_stuff(); remove_lockfile(); exit;

just be sure to install your signal handler(s) after you test for file locking... race conditions really suck.

dirty...

Replies are listed 'Best First'.
RE: Re: Locking a script
by merlyn (Sage) on Oct 19, 2000 at 08:42 UTC
    That's not the traditional method, because the traditional methods work. {grin}

    You can't separate the steps of "test for a lockfile" from "create a lockfile". It has to be an "atomic" operation. See the massive literature on this.

    -- Randal L. Schwartz, Perl hacker

      Sure, it's called open(). I was just commenting on what is actually happening. To a newbie you just cutting out some code, it's not essential to know that this is an atomic kernel call.
      AgentM Systems nor Nasca Enterprises nor Bone::Easy is responsible for the comments made by AgentM.</h6>
        Uh, I see the testing step at the top of the program, then the create-a-lockfile later. Maybe you knew that this wasn't the real logic of the program, but you sure didn't communicate that in a way that wasn't going to bite the newbies.

        -- Randal L. Schwartz, Perl hacker