If the pid file is removed for some reason, a next invocation can falsely determine no other invocation is running. OTOH, if an invocation dies unexpectedly, it may leave behind the pid file.
If the pid contained in the pid lock file is not in the process table, you should display a message asking if the process is running. If not, you can prompt the user to remove the file.

If the file does not exist (false negative), there is a logic error in your program (the pid lock file should be the first object created and the last destroyed).

You'd have to use file locks. But then you may as well use file locks in the first place.
The point of using a predictable lock file (program_name.pid) in a predictable location (/var/run ?) is to handle all of the situations you describe. Data files used by the program may or may not be predictable. Directory locations may or may not be predictable (relative vs. absolute paths).

Using the conventions that have grown over the years (/var/run for pid lock files) work for a reason. YMMV


In reply to Re^3: How can i check my script is already running? by proceng
in thread How can i check my script is already running? by shekarkcb

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.