At 01:46 PM 12/15/00 +0100, you wrote:
>On Fri, 15 Dec 2000, Kees Vonk 7249 24549 wrote:
>
> > Stas,
> >
> > I had the following in my code:
> >
> > my($nOrgPID) = fork;
> > exit if $nOrgPID;
> > die "Could not fork: $!" unless defined $nOrgPID;
> >
> > close STDIN;
> > close STDOUT;
> > close STDERR;
> > setsid() or die "Could not start new session: $!";
> >
> >
> > but that didn't work, however when I changed it to this:
> >
> > my($nOrgPID) = fork;
> > exit if $nOrgPID;
> > die "Could not fork: $!" unless defined $nOrgPID;
> >
> > require 'sys/syscall.ph';
> > for (my $i=0; $i<=255; $i++) {
> > syscall(&SYS_close, $i + 0); # must force numeric
> > }
> >
> > setsid() or die "Could not start new session: $!";
> >
> >
> > the socket got released and I could restart the server. I
> > know it is a little crud, but it seems to work.
>
>But you don't need to call setsid() when you fork. Why looking for
>complicated workaround when you can do it properly without any workaround.
>Have you ever seen an example of fork that uses setsid?
Yes, the following is taken straight out from the perlipc documentation:
-----------------------------
Complete Dissociation of Child from Parent
In some cases (starting server processes, for instance) you'll want to
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 /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: $!";
}
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 /dev/tty and use the TIOCNOTTY
ioctl() on it instead. See tty(4) for details.
-----------------
Am I missing something?
Regards,
Rafael Caceres