Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I am using the following method to fork off and dissociate from the terminal for a script I want to daemonize. Now, I'm told I could just do some while(1) { sleep; } trickery, but I would think there'd be a way to do it without resorting to that. The script should last forever as is, as it uses File::Tail to watch a log. Permissions are OK, as if I remove the sighandler stuff, I can watch it output exactly what I'm looking for.
defined(my $pid = fork) or die "Can't fork: $!\n"; exit if $pid; setsid or die "Can't start a new session: $!\n"; + + $SIG{'HUP'} = $SIG{'INT'} = $SIG{'KILL'} = $SIG{'TERM'} = &sig_handler +; my $connection_log = "/var/log/qmail/qmail-smtpd/current"; if(-e $connection_log){ my $file = File::Tail->new($connection_log); my $line; while (defined($line = $file->read)) { print "Line contents: $line\n"; } } sub sig_handler { my $sig = shift; print "Received SIG $sig\n"; quit(0); }
Now, I would think that it would run forever (as long as the file was moving) and occasionally print the signals it received, except it dies immediately because the fork kills it. Does someone have a tutorial or a link they could present to do what I think I'm trying to do? Alternatively, education as to WIDIAW (why i'm doing it all wrong) would also be appreciated :) Does the file being rotated out kill File::Tail? The documentation suggests that it handles that situation. Update: It's fairly obvious why it's quitting to me (quit) Forget that part, the question still stands, am I daemonizing the wrong way?

In reply to fork process + SIG handlers by cidaris

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 wandering the Monastery: (5)
As of 2024-04-23 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found