Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Daemons???

by Zaxo (Archbishop)
on Oct 26, 2001 at 13:12 UTC ( [id://121604]=note: print w/replies, xml ) Need Help??


in reply to Daemons???

Here is the basis for a general daemon. Add code at the end to do what you want. This follows the Stevens rules.

#!/usr/bin/perl -w use strict; use POSIX qw( setsid ); my $debug = 1; my $logfile = q(.testlog); # Season to taste my @fh_unused = (\*STDIN, \*STDOUT); open \*STDERR, ">> $ENV{'HOME'}/$logfile"; select((select(\*STDERR), $| = 1)[0]); { # Daemon Rule 1) Fork and exit the parent. my $ppid = $$; my $pid = fork and exit 0; ! defined $pid and die "No Fork: ", $!; while (kill 0, $ppid) { select undef, undef, undef, .001; }; } # Daemon Rule 2) become session leader, pg leader, no term my $session_id = POSIX::setsid(); # Daemon Rule 3) cd to / chdir '/' or die "Could not cd to rootfs", $!; # Daemon Rule 4) set file creation mask to 0 my $oldmask = umask 00; # Daemon Rule 5) Close unneeded file handles close $_ or die $! for @fh_unused; # Your code here exit 0;
If you open communication channels before the fork, the child will inherit their handles.

After Compline,
Zaxo

Log In?
Username:
Password:

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

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

    No recent polls found