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

I'm basically setting up a dayz arma server on linux. I wrote a perl script to read the output from the server and interact with a mysql database.

I've tested the perl script by passing it typical output saved in a text file.

hive.pl < hive.test

The perl portion works fine.

Here is my problem. No matter how I try this from a shell script, I can't get the perl script to start and read the output (no errors). Here is the basics of what I've got.

ARMA_DIR=/home/dayz/chernarus_server LOGFILE=${ARMA_DIR}/log.${PORT}.txt HIVE=${ARMA_DIR}/linux/hive.pl SERVER=${ARMA_DIR}/server ${SERVER} 2>&1 | tee ${HIVE} ${LOGFILE}

I don't really need the tee to the logfile, I could just go to the perl file. However, I added this just to make sure of what was coming out.

I'm using something like the following inside the perl file to read. (left out all the actions inside while)

while (<STDIN>) { }
Any guidance?

Replies are listed 'Best First'.
Re: Redirect Shell Launch to perl script
by haukex (Archbishop) on Nov 27, 2016 at 08:58 UTC

    Hi delpi,

    Without knowing anything about the server, it's a little difficult to give a good answer, also you don't tell us how you know your Perl script isn't working - e.g. you could have it write debug output to a separate logfile.

    Anyway, I have two ideas. First, if you can get the server to write its output to a log file which you can then view live with tail, you can do the same with Perl, with the module File::Tail. That has the advantage of decoupling the two processes, at the expense of a small bit of performance.

    Second, you could try invoking the server with something like IPC::Run, which can communicate with the subprocess interactively. The disadvantage is that now the server is a subprocess of the Perl script, which might not work if, for example, the server tries to detach itself and go into the background (daemonize). Anyway, a simple example:

    use IPC::Run qw/run/; run ['/path/to/server'], \undef, sub { my $in = shift; chomp($in); print "<<$in>>\n"; };

    However, depending on how the server is implemented, this solution may still suffer from buffering.

    Hope this helps,
    -- Hauke D

      I'll have to explore that for another project I'm working on. Thanks.
Re: Redirect Shell Launch to perl script
by LanX (Saint) on Nov 27, 2016 at 02:40 UTC
    You don't show us the Perl script and I never heard of your application.

    (update: And it's not clear if you tested it without tee.)

    Does this work?

    ${SERVER} 2>&1 > hive.txt; cat hive.txt | hive.pl

    If yes, then it might be related to buffering, or missing newlines or redirection problems or ...

    So what is the content of hive.txt?

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      That part I posted about STDIN was the only relevant part of the perl script. I event tried it with only that and a line to print to log. However, it turned out at some point I fixed the Shell Script, but at that point the perl script had gotten overwritten by log information. It is all working now.
Re: Redirect Shell Launch to perl script
by FreeBeerReekingMonk (Deacon) on Nov 27, 2016 at 09:15 UTC
      I'll give that a try.
Re: Redirect Shell Launch to perl script
by soonix (Chancellor) on Nov 27, 2016 at 16:42 UTC
    in
    ${SERVER} 2>&1 | tee ${HIVE} ${LOGFILE}
    I'd expect tee to overwrite your ${HIVE} script. Why don't you
    ${SERVER} 2>&1 | tee ${LOGFILE} | ${HIVE}
    ?
      Why don't I? Obviously because I don't know that fact and it is why I asked for help.
Re: Redirect Shell Launch to perl script
by Anonymous Monk on Nov 27, 2016 at 02:45 UTC
Re: Redirect Shell Launch to perl script
by delpi (Novice) on Nov 27, 2016 at 18:18 UTC
    After some troubleshooting, I did something dumb. At one point when was testing this I managed to overwrite the perl script with the log information. No clue how, but it works fine now. Thanks to everyone for the help.

      After some troubleshooting, I did something dumb. At one point when was testing this I managed to overwrite the perl script with the log information. No clue how, but it works fine now. Thanks to everyone for the help.

      If you give tee filename it will gladly stuff it with stuff , study the previously linked gnu tee examples

      $ echo echo |tee 1 2 3 4 5 6 7 8 echo $ cat 1 2 3 4 5 6 7 8 echo echo echo echo echo echo echo echo