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

How do I script Perl to continuously run? I want to have a script listen to multiple ports and log data from each port.

Replies are listed 'Best First'.
Re: Background Run
by BrowserUk (Patriarch) on Sep 05, 2015 at 01:47 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Background Run
by hippo (Archbishop) on Sep 05, 2015 at 11:08 UTC
    How do I script Perl to continuously run?

    Avoid any terminating statements such as exit or die, handle all signals, wrap any calls which may cause termination in eval (or use one of the try/catch modules), ensure you don't exhaust the limited resources of the hardware or the limits assigned to the process by the O/S. If using threads, ensure everything is thread-safe. If you do all that and your script still fails to run continuously then you'll need to provide some more detail.

Re: Background Run
by stevieb (Canon) on Sep 05, 2015 at 01:17 UTC

    Welcome to the Monastery, avatarengineer!

    Can you describe in a bit more detail as to what you're trying to achieve? Are you wanting to write a script that can listen on multiple ports, and write/log information if those ports are touched? Or are you wanting to develop something that tries to 'see' when existing listening services receive requests and then logging that?

    Beyond that, can you share what you've tried so far, or at minimum, show what/where you've researched?

    -stevieb

Re: Background Run
by 1nickt (Canon) on Sep 05, 2015 at 01:10 UTC

    Hi, that's no problem, see this link.

    The way forward always starts with a minimal test.

      Please don't use URL-shorteners, especially not lmgtfy. This is not the tone I want used for newcomers. Also, if somebody asks what to you seems like a very basic question, most likely they also lack the ability to judge the quality of the links found through a search engine. Please, when linking to a search query, also point out two or three relevant links from the results and maybe even explain what to find in them or why that's relevant.

Re: Background Run
by Anonymous Monk on Sep 05, 2015 at 23:56 UTC

    This seems to work:

    use warnings; use strict; use IO::Socket; my $socket = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '8888', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $socket; $|++; while (1) { my $incoming = $socket->accept(); while(<$incoming>) { print $_; } print "\nNext session.\n"; } close($socket);

    Here is how I run it as a daemon in Unix:

    $ nohup ./listen.pl >listen.log 2>listen.log &

    After it is running, here I am testing it out:

    $ telnet localhost 8888 Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. You there? ^] telnet> quit Connection closed.

    And here is the output showing up in my log:

    $ cat listen.log You there? Next session.

    And here is the process still running afterwards.

    $ jobs [1]+ Running nohup ./listen.pl > listen.log 2> listen.log &