one common way of doing this sort of stuff is to have your script write a PID (process ID) file. when you start up your process, your script will check for this file, if the file is there, a copy of it is already running. if the file is missing, your script will create the PID file and continue to do whatever it's designed to do. if you go with this approach, make sure you create your PID file in automic mode to avoid race condition and make sure you clean up the PID file when your script exit (either normally or ab-normally).

another common approach is to search the process table for the script and exit if it find it there. something like this:


#!/usr/bin/perl -w
use strict;

use Proc::ProcessTable;

exit if already_running();

#--
#-- your script continue
#--

sleep(15);
print "hello world\n";

sub already_running
{
  #--
  #-- the following says:
  #--
  #-- 1. if the name of the script is similar
  #-- 2. if the process ID is different
  #--
  #-- you will write your own logic here to catch
  #-- the script you are looking for. don't just
  #-- copy and paste it without knowing what it does.
  #-- if all of your scripts come from a common parent
  #-- process, you might want to use $_->{ppid} instead
  #--
  $0 =~ /$_->{fname}/ && $_->{pid} != $$ && return 1
    for (@{Proc::ProcessTable->new->table});

  return 0;
}

__END__

$ ./proc.pl &
$ ./proc.pl
$ ./proc.pl
hello world
[1]+  Done                    ./proc.pl
$

notice hello world is printed only once. the second and the third time i ran the script, they exit immediately because the first script is still running.

BenBart wrote:
Hi all,

How do I prevent multiple instance of the same script from running?

That is for example, if I have a script named script1.pl and it is already running, I want the script to be able to check that it is already running and then just exit ...

Is this possible to do in both UNIX and Windows?



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to