Some problems I see with the above solutions are 1) you have to determine the pid(s) of the potentially interfering processes; 2) you have make sure that the processes assigned to those pids are instances of your daemon. You'll have trouble if your daemon exits and another process is assigned the same pid.

One classic way of handling this problem is to have the daemon acquire a lock on a specific resource, and usually things are set up so that the lock is automatically released when the process exits. The challenge in your case is to come up with a command line -> resource mapping which will do what you want.

One simple idea is to map the command line to a file path. For instance, map "-a 1 -b 2 -c 3" to "/some/dir/locks/-a 1 -b 2 -c 3". Then use flock or File::lockf to obtain an exclusive lock on that file. You'll have to make sure that the path will work in all cases (e.g. be careful of special characters in your command line, path length, etc.) Another option is to use mysql's locking feature which allows you to identify a resource by an arbitrary string.

Some issues I'll think you'll run into:

In general when I've had to do this I've only needed a couple of different locks per daemon -- most of the time just one. If you tell us more about your application and its possible command line arguments, we can advise you on the what the best locking mechanism would be.


In reply to Re: Detect options of script runnning in a demon mode by pc88mxer
in thread Detect options of script runnning in a demon mode by knbknb

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.