From: Anton Berezin Newsgroups: fido7.ru.perl Subject: Re: Как запустить процесс на Date: 29 Jun 1999 23:22:37 +0400 Message-ID: <19990629211920.C4166@lion.plab.ku.dk> References: <1699227027@mail.nevalink.ru> <37771825.36B2015D@vrn.sterling.ru> <86k8sndz0o.fsf@dmhome.orsk.ru> <3778E1A8.EF9DE220@vrn.sterling.ru> In-Reply-To: <3778E1A8.EF9DE220@vrn.sterling.ru>; from Denis Tsyplakov on Tue, Jun 29, 1999 at 07:31:30PM +0400 On Tue, Jun 29, 1999 at 07:31:30PM +0400, Denis Tsyplakov wrote: > Привет! > > > sub run { > > return unless fork() == 0; > > exec ($command); > > } > > Хм. А что будет, если родительский процесс завершится? > Т.е. может быть все в порядке, но интересно. Для того, чтобы гарантированно не случилось ничего плохого (случиться может, не сумлевайся; при ряде дополнительных условий связанных с _группами процессов_ и _сессиями_) нужно использовать двойной fork() плюс setsid(2) (из модуля POSIX, в случае Perl). Впрочем, чем объяснять самому, предпочту процитировать кусочек из perldoc perlipc. А то что-то эта нить не в ту степь залезла - какой-то ликбез по shell job control получается. :-) ----------------------------------- 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 the tty(4) manpage for details. Non-Unix users should check their Your_OS::Process module for other solutions. ----------------------------------- Hope this helps, -- Anton Berezin Читающий документацию да обрящет...