Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!ux1.cso.uiuc.edu!csrd.uiuc.edu!sp1.csrd.uiuc.edu!kai From: kai@sp1.csrd.uiuc.edu (Kuck And Associates) Newsgroups: comp.lang.perl Subject: How do I detach from control terminal? Message-ID: <1990Aug6.190050.2706@csrd.uiuc.edu> Date: 6 Aug 90 19:00:50 GMT Sender: usenet@csrd.uiuc.edu (news) Distribution: comp Organization: UIUC Center for Supercomputing Research and Development Lines: 42 I've written in Perl the equivalent of the C code I have used in a daemon to detach from the control terminal, but it doesn't seem to work (on a BSD Unix host). Can anyone tell me what I'm doing wrong in this example? Patrick Wolfe (pat@kai.com, kailand!pat) System Programmer/Operations Manager, Kuck & Associates "Any opinions expressed are my own, not my employers's. Please don't call my boss and complain ... again." -- cut here -- cut here -- cut here -- cut here -- #!/usr/local/bin/perl3 $logfile = "/dev/null"; do detach (); sleep (600); exit (0); # behave like a proper daemon - set our own process group and detach from our control terminal sub detach { chdir ("/"); open (STDIN, "< /dev/null") || die "cannot redirect stdin from /dev/null: $!"; open (STDOUT, "> $logfile") || die "cannot redirect stdout to $logfile: $!"; open (STDERR, ">&STDOUT") || die "cannot dup stderr to stdout: $!"; select (STDERR); $| = 1; select (STDOUT); $| = 1; if (open (TTY, "> /dev/tty")) { do "sys/ioctl.h"; # probably /usr/local/lib/perl/sys/ioctl.h if ($@ ne "") { print STDERR "check_uptime: cannot \"do sys/ioctl.h\": $@\n"; print STDERR "you probably need to run \"makelib sys/ioctl.h\"\n"; exit (1); } ioctl (TTY, $TIOCNOTTY, 0); close (TTY); } setpgrp (0, $$); }