Hello Perl IPC Wizards,

I have written a script to do some Unix/DBMS admin tasks while running as a daemon.
I used the POSIX::setsid() call and did the usual preparations as suggested in "perldoc perlipc" to auto-background.

Before that however I created a kind of semaphore file that holds the child's new PID of the daemon process and aqcuired an exclusive lock on it.

Despite, when I run the script anew while one daemon is running the flock call (that I or'ed to LOCK_NB in order to send a warning mail or something similar before the die()) returns a true value which screws my intended logic (provided there is any) altogether.

First I thought the reason lies in my usage of localized globs for the filehandles that could go out of scope, and thus close my semaphore file inadvertently.
But this can't be since I keep the refcount alive by returning the file handle globs from the initilization sub which are assigned to lexically scoped variables by the caller.

I also keep on logging to another file through one of these variables. So the refernces must still be valid.

Is the backgrounded child somehow losing the file lock by becoming a session leader?

I ask just for curiosity because I could easily insert some logic that sends a "kill 0 => $pid" to the PID read from the semaphore file to check if another deamon is running already. But I rather would like to do it by the use of file locks.


In reply to Keeping File Locks after Daemonization by SIGSEGV

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.