On Mon, March 30, 2009 12:03 am, Sisyphus wrote:
> From: "Jon Bjornstad" <j...@logicalpoetry.com>
>> 1 - How to ensure that there are not two versions of my script running?

I use a Mutex to accomplish this:

http://search.cpan.org/~cjm/Win32-IPC-1.07/lib/Win32/Mutex.pm

use Win32::Mutex;

my $mutex;
my $mutex_name = 'SCRIPTX';

if (Win32::Mutex->open($mutex_name)) {

   die "[Error] PROGRAM INSTANCE ALREADY RUNNING: [$mutex_name]";

} else {

   print "MUTEX CHECK SUCCESS: [$mutex_name] CREATING NEW MUTEX\n";
   $mutex = Win32::Mutex->new(1, $mutex_name);

}


> One way is to have your script do something that prevents multiple
> instances of it from being run. Something like:
>
>
> ##################################
> use warnings;
>
> my $remove = 1;
>
> if(-f 'script.lockfile') { $remove = 0; # prevents removal of lockfile
> die "lockfile exists\n"; }
>
>
> open(WR, '>', 'script.lockfile') or die $!; close WR or die $!;
>
> # Do the stuff you want to do here
>
>
> END {
> if($remove) { warn "lockfile not removed" if !unlink 'script.lockfile'; }
> };
> ##################################
>
>
> Of course, if you're worried that someone might manually remove the
> lockfile so that they can run multiple instances of the script, then that
> solution won't do. (Or maybe someone might manually create a
> 'script.lockfile' file
> so that *no* instances of the script can be run.)
>
> And if there's a system crash while the script is running then the
> lockfile may not get removed automatically.
>
> Another thing you can do is have the script connect to a socket. That
> way, a second instance of the script will try to connect to the same
> socket ... and fail as the socket is already in use. (Of course, if
> there's another entirely different process using the same port then you
> won't be able to run even one instance of the script.)
>
> There are probably more sophisticated solutions, if needed.
>
>
> Cheers,
> Rob
>
>
> _______________________________________________
> Perl-Win32-Users mailing list
> Perl-Win32-Users@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
>


_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to