Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Messin around with fork() and fd's

by snafu (Chaplain)
on Oct 29, 2001 at 00:16 UTC ( [id://121844]=perlquestion: print w/replies, xml ) Need Help??

snafu has asked for the wisdom of the Perl Monks concerning the following question:

Ok, so I think I kinda got the hang of fork() (be gentle, today was my first time ever trying it (: ) but what's say I have a program that I want the maintainer to have the choice to daemonize or not. What's say, more, that if the script daemonizes all output should go to a file log vs STDOUT. Otherwise, all output goes to STDOUT (or STDERR of course which means that STDERR needs to go to a logfile too if we daemonize). How would I do this?

My current forking code...

## Should this program daemonize... ## ie: should it run all the time ## in the background? my $daemonize = 1; ################################################## # fork code if ( $daemonize ) { my $pid = fork(); die("fork() failed: $!") unless defined $pid; ## need to do something right here ## to get STDOUT and STDERR to go ## to a file vs the console/screen. if ($pid) { exit(0); } }
----------
- Jim

Replies are listed 'Best First'.
Re: Messin around with fork() and fd's
by chromatic (Archbishop) on Oct 29, 2001 at 00:51 UTC
    Several possibilities:
    1. Don't worry about it. Let the user handle this, using standard Unix redirection. This is the easiest possibility for you, and probably the most flexible for users.
    2. Ignore STDERR for now (if run from cron, that output will be mailed to the job owner), and use two-arg select to change the default output filehandle.
    3. Re-open STDOUT and STDERR elsewhere:
      open(STDOUT, '>daemon.log') or die "Can't open daemon.log: $!"; open(STDERR, '>daemon.err') or die "Can't open daemon.err: $!";
    4. Skip the forking altogether. This may not be an option, but I don't see what it adds to the process, unless you're trying to get init as your parent. Then it makes sense. :)
Re: Messin around with fork() and fd's
by wog (Curate) on Oct 29, 2001 at 00:49 UTC
    Redirecting a filehandle is easy especially if you don't care about restoring it later:

    open STDOUT, ">>somelogfile" or die "opening logfile: $!\n"; open STDERR, ">&STDOUT" or die "dup'ing STDOUT to STDERR: $!\n";

    Basically, this is the same as example of redirecting STDOUT and STDERR in the docs for open except you don't actually save the filehandles beforehand and restore them afterwards.

Re: Messin around with fork() and fd's
by Zaxo (Archbishop) on Oct 29, 2001 at 07:17 UTC

    There is commented code for daemonizing a process at Re: Daemons???. It can be wrapped in sub possess {...} and set up to take arguments. Syntax might be:

    possess ( LOG => "/the/log/file", INP => [], # aryref to input handles OUT => [], # output handles FAT => [], # close these handles ENV => { PATH => ":" }, SIG => { }, # signal handlers hashref );

    After Compline,
    Zaxo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://121844]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (1)
As of 2024-04-25 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found