I make my scripts write out their PID to a file when they start after they have checked they are unique. The check consists of seeing if the pid file exists, reading it and checking if the process on that PID looks like an instance of my script (just in case the OS re-used the pid for something else) clean exit will clear up the pid file. Here is the check...

# See if I am already running # $my_name was derived earlier from $0 # $pidfile is set up in the scripts headers if (-f $pidfile) { open PID, $pidfile; my $pid = <PID>; close PID; print "read pid $pid\n" if $debug; open PROCS, "ps -ef |"; <PROCS>; # loose the header while (<PROCS>) { (undef, my $this_pid) = split; if ($this_pid == $pid) { print "pid in use $_\n"; # check this realy is us, not pid re-use by OS last unless /$my_name/; die "looks like an instance is already running, exiting\n" +; } } } open PID, ">$pidfile"; print PID $$; close PID;

That is the framework I use, it is a cut and paste from a larger script so will not run in isolation, sorry it is Friday and I want to go home so I can pack for my sailing holiday that starts tomorrow SEG

Cheers,
R.


In reply to Re: Checking for multiple instances by Random_Walk
in thread Checking for multiple instances by MarkNo1

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.