Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
hi
to ensure 1 instance of the application running using perl
i have a suggestion
1. pls open a specific port for locking
port must be greater than 10000 and less than 60000
ex: port 28004
2. so as long as the port is open, then, meaning there is still program running on that particular port
3. upon program exit, close the port
i have an ex code below:
hope this helps
its a class
how to use??



#-----------------------------------------------------


use PortLocker();
my $port = 52480;
my $locker = new PortLocker();
if(! $locker->lock())
{
die("Port Already lock, pls check if already running!\n");

#--- failed, port already locked
}
#--- continue here your program

. #then before exit, pls unlock the port

$locker->unlock();


#-----------------------------------------------------






PortLocker.pm ( code )

#----------------------------------------------------------------------------------------------
# Filename : PortLocker.pm
# Description : this is the package that will lock a specified port#
# Date : 08-10-2001
# Ver : ver1.1
# Author : bayugyug
#----------------------------------------------------------------------------------------------



package PortLocker;
use IO::Socket;

#--- set vars here
my $portlock = undef;
use constant PORT_LOCK => 28788;


#---
$| = 1;
my $VERSION = "1.01";





sub new
{


my $type = shift;
my $self = {};
$self->{'PORT'} = $_[0];
$self->{'PORT'} = PORT_LOCK if(int($self->{'PORT'}) <= 0);
bless $self, $type;

}



sub lock()
{

my $self = shift;
my $port = $self->{'PORT'};


$portlock = new IO::Socket::INET (
LocalHost =>
'localhost',
LocalPort => $port,
Proto => 'tcp',
Listen => 1
);

$self->debug("lock() :: port ($port) ok");
#--- return ok/fail

unless( $portlock )
{
return 0;
}

return 1;
}


sub unlock()
{

my $self = shift;
if(defined($portlock))
{
close($portlock);
}

$portlock= undef;
$self->debug("unlock() :: ok");
}


#---
sub debug()
{
my $self = shift;
my (@parm) = (@_);
my $msg = join(' : ',@parm);
my $scr = "Ver$VERSION";

print "$scr - $msg\n";
}


1;


(END) #----------------------------------------------------- hope this helps, tnx #!/bin/bayugyug


In reply to Re: Ensuring only one copy of a perl script is running at a time by bayugyug
in thread Ensuring only one copy of a perl script is running at a time by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (7)
    As of 2015-07-07 00:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (85 votes), past polls