ayhank has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to write a PERL daemon in which I want to read all inputs from all ttys and print it to STDOUT (redirected to log) (deamonize the program and read all command line inputs as SDTIN independent of TTYs).

How to read all inputs from all ttys(or from keyboard independent of ttys) and print it to STDOUT to log it into a file "logy"?

I have a code below, part of it I found on the net and part of it I tried to modify to achive my goal. Please help me understand what is wrong in the below code and how to fix it? Thanks in advance

#!/usr/bin/perl -w use strict; use diagnostics; use Carp; use integer; # Completely dissociate from the parent tty and close file descriptors +.<br> 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); }

Edit: by BazB. Added code tags.

  • Comment on perl daemon, redirect STDIN to read from all ttys and print to STDOUT
  • Download Code

Replies are listed 'Best First'.
Re: perl daemon, redirect STDIN to read from all ttys and print to STDOUT
by zentara (Cardinal) on Mar 16, 2005 at 13:53 UTC
    It looks like you are trying to make a keystroke recorder?

    Maybe this c program will help you, look at the c code and see what they do? sudosh

    If you get your perl version working, be sure to post it. :-)


    I'm not really a human, but I play one on earth. flash japh