diff options
author | Roderick Schertler <roderick@argon.org> | 1998-10-29 09:50:18 -0500 |
---|---|---|
committer | Gurusamy Sarathy <gsar@cpan.org> | 1998-10-30 19:38:15 +0000 |
commit | 893af57aeb05cb9ca3ec13a262068d56558e4325 (patch) | |
tree | 587f57add35f485e05195e7af6be5202ffea973b | |
parent | 21e725129f7284f543ce260557cc7d475bd76241 (diff) | |
download | perl-893af57aeb05cb9ca3ec13a262068d56558e4325.tar.gz |
patch for daemonization docs in perlipc
Message-ID: <17625.909690618@eeyore.ibcinc.com>
p4raw-id: //depot/perl@2149
-rw-r--r-- | pod/perlipc.pod | 63 |
1 files changed, 25 insertions, 38 deletions
diff --git a/pod/perlipc.pod b/pod/perlipc.pod index 59c5ad9f01..cc2a1a9d81 100644 --- a/pod/perlipc.pod +++ b/pod/perlipc.pod @@ -317,46 +317,33 @@ details). =head2 Complete Dissociation of Child from Parent In some cases (starting server processes, for instance) you'll want to -complete dissociate the child process from the parent. The easiest -way is to use: - - use POSIX qw(setsid); - setsid() or die "Can't start a new session: $!"; - -However, you may not be on POSIX. The following process is reported -to work on most Unixish systems. Non-Unix users should check their -Your_OS::Process module for other solutions. - -=over 4 - -=item * - -Open /dev/tty and use the TIOCNOTTY ioctl on it. See L<tty(4)> -for details. - -=item * - -Change directory to / - -=item * - -Reopen STDIN, STDOUT, and STDERR so they're not connected to the old -tty. - -=item * - -Background yourself like this: - - fork && exit; - -=item * - -Ignore hangup signals in case you're running on a shell that doesn't -automatically no-hup you: +completely dissociate the child process from the parent. This is +often called daemonization. A well behaved daemon will also chdir() +to the root directory (so it doesn't prevent unmounting the filesystem +containing the directory from which it was launched) and redirect its +standard file descriptors from and to F</dev/null> (so that random +output doesn't wind up on the user's terminal). + + use POSIX 'setsid'; + + sub daemonize { + chdir '/' or die "Can't chdir to /: $!"; + open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; + open STDOUT, '>/dev/null' + or die "Can't write to /dev/null: $!"; + defined(my $pid = fork) or die "Can't fork: $!"; + exit if $pid; + setsid or die "Can't start a new session: $!"; + open STDERR, '>&STDOUT' or die "Can't dup stdout: $!"; + } - $SIG{HUP} = 'IGNORE'; # or whatever you'd like +The fork() has to come before the setsid() to ensure that you aren't a +process group leader (the setsid() will fail if you are). If your +system doesn't have the setsid() function, open F</dev/tty> and use the +C<TIOCNOTTY> ioctl() on it instead. See L<tty(4)> for details. -=back +Non-Unix users should check their Your_OS::Process module for other +solutions. =head2 Safe Pipe Opens |