Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
can't figure a good way to hold the pidfile locked while i daemonize since Proc::Daemon closes open files (losing the locks)

This is expected behaviour for Proc::Daemon or indeed any daemon written in line with the recommendations for daemon processes in "Advanced Programming in the Unix Environment" by W. Richard Stevens - This behaviour results from the closure of all open file handles, specifically, the line:

foreach $i (0 .. OpenMax) { POSIX::close($i); }

The best way to avoid this behaviour affecting your lock files is to acquire the lock after becoming a daemon process. If this lock acquisition fails, write to the application log and exit - This is relatively standard practice. For example, the Apache web server does something similar with regard to open a listening socket connection - The server will daemonise before opening this socket, which if fails, will write the error to the log file and exit.

Proc::PID::File has some issues

I would note that there is also a race condition between the opening of the file and acquisition of the lock and that the return value of the flock function is not checked to ensure that the lock acquisition has been successful.

 

perl -le 'print+unpack("N",pack("B32","00000000000000000000001000111100"))'


In reply to Re: Re: How to get a persistent daemons after exiting the session? by rob_au
in thread How to get a persistent daemons after exiting the session? by hackmare

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (1)
As of 2024-04-24 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found