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

We have a machine at work that runs diagnostic in a command tool. This machine is on Solaris 5.8 and uses Perl 5.8.8

What I am wondering is if I could use Perl to watch the STDOUT of the command tool that runs the diagnostics so that I could save/parse certain data automatically? I only want to passively listen. I realize this would constitute some security issues as a general behavior, so I am expecting you to say that terminal sessions have limited access. But I thought I would ask.

I have to be able to watch the terminal from another terminal session, because the program is started by using a shortcut. I can't run the perl script before hand in the same session nor execute it from Perl. The diagnostic program has to be executed from the shortcut.
  • Comment on Can Perl read another terminals STDOUT?

Replies are listed 'Best First'.
Re: Can Perl read another terminals STDOUT?
by roboticus (Chancellor) on Apr 16, 2014 at 21:44 UTC

    westrock2000:

    It's not terribly difficult, depending on what you want to do. If you're just running a program and are content to process the STDOUT information after the program finishes, you can get an array of all the lines like this:

    my @text = `command to run`;

    This will run the command, collect the STDOUT data and put it into @text, where you can do with it what you want. In this scheme, your perl script needs to run the command. If you'd rather not have perl run the command, you can do something like this:

    command to run | perl script_to_process_STDOUT.pl

    Here you're just telling the shell to run a command and feed its STDOUT to the input stream of the next program. Then your script would read the data as if it were being typed by hand.

    You can review the modules IPC::Cmd, IPC/Open2 and IPC/Open3 on CPAN for other ways to do it. If you look around there, you'll likely find a few others, too.

    Update: It seems I missed the last paragraph of your message. Perhaps tcpsnoop might be able to help. The ttysnoop.c client looks like it would be easy enough to modify to start a perl script and feed it the data from the other TTY. I don't know if it'll run on Solaris or not, but I don't see any obvious blockers.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Can Perl read another terminals STDOUT?
by mhearse (Chaplain) on Apr 16, 2014 at 23:57 UTC
    I would suggest running the cron job on a detached screen. Or use netcat.

    On Solaris machine running cron, edit cront job to redirect stdout to netcat:

    uptime | nc [remote ip addr] 65400

    On the remote machine:

    nc -l 65400

    You could do all of this in Perl too.

Re: Can Perl read another terminals STDOUT?
by Anonymous Monk on Apr 16, 2014 at 21:43 UTC
Re: Can Perl read another terminals STDOUT?
by zentara (Cardinal) on Apr 17, 2014 at 14:58 UTC
    Just as point of interest, if you are the owner of the pid, which is running the program you wish to watch, you should be able to access it's file descriptors, of which fd 1 is STDOUT. On linux, it would be /proc/$pid/fd/1.

    See Duping filehandles and perldoc q dup.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Your idea looks promising. As I understand it, the fd/1 is somewhat like a pipe?

      I however could not figure out how I was supposed to get to that data stream.

      I wrote a perl script that incremented by 1 each second indefinently in a terminal and then went to another terminal and ran the following script:

      #!/usr/bin/perl #Get PID of process to monitor print ("PID: "); $fd=<>; print ("$fd\n"); open(FD, "<&=$fd"); @array = <FD>; close(FD); print ("@array\n");


      Contents of @array is blank. So I do not think I am understanding how to access that stream.
        I believe it is a bit more complicated than just using @array = <FD>, because it is an ongoing stream until the program exits or closes it's stdout. You probably will need to use a select loop on the filedescriptor, or somehow attach to the output.

        If you google for duping stdout linux there are some good links.

        If you figure it out, please let us know. You may have to launch your watched program with a tee tapping off the stdout to another filedescriptor, which you can then read. Good luck.

        P.S. This is not the best way to solve your problem, only a novel way. Using IPC to run the program, is way easier.


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