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.
Yes, but that's an if. And you conveniently left off the next line:
By the time the program is invoked again, the pid may have been reused;
So, we now have a pid file, with the pid it contains in the process table. Now what?
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).
I'm sure you are aware there are OSses that allow processes to remove files they haven't written to. I'm unware of any programming technique that prevents other processes (for instance, sometime typing 'rm file') from removing files. Please enlighten us, so we never have to make such logical errors in our programs again!
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.
Hmmm, a pid file is a data file. It contains data, doesn't? Data you're using to determine whether an instance is running or not.
Using the conventions that have grown over the years (/var/run for pid lock files) work for a reason. YMMV
I've been around long enough to know that while pid files often work, they sometimes don't. For the reasons I explained. You've never had to remove a pidfile to allow a program to start? Get some more experience.

In reply to Re^4: How can i check my script is already running? by JavaFan
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.