Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

nohup & PERL.

by Anonymous Monk
on Apr 16, 2002 at 23:25 UTC ( [id://159675]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I'm writting a program that uses netcat as a simple network communication interface. (see http://www.stud.math.ntnu.no/storedoc/netcat/.) However, I am running into a problem where netcat remains running even after the parent PERL program that calls it is killed with a -15 or -9. I'm wondering if it is netcat itself or the way that I am calling netcat from PERL that is causing it to ignore the fact that its parent has died. The specific I call netcat as follows: if ( open(NETCAT,"nc -l -p $SSPORT | ") ) {} Is there anyway to make netcat die when the parent PERL program dies?

Replies are listed 'Best First'.
Re: nohup & PERL.
by belg4mit (Prior) on Apr 16, 2002 at 23:35 UTC
    Not having netcat to attempt any testing, you should probably be saving the return value of open which is luckily the PID of your child (netcat). You might then try explicitly kill-ing the child before you exit.

    You might find perlipc helpful as well. Specfically the TCP client, which ould allow you to forgo the use of netcat.

    --
    perl -pe "s/\b;([mnst])/'\1/mg"

      Thanks this is good information. Unfortunately, the programs are on two servers across a network so IPC won't work. Also, the problem is not a normal death. It is when a kill comes from the outside. Is there a way to capture all signals? If so, then I could explicitly kill the netcat process, otherwise I'm still toast. What I was really hoping for was a way to call a process from PERL that forces the child process to die if PERL, the parent process, dies. Thanks, Daniel
        Again I recommend reading perlipc, as hossman has seconded. The other things he also suggests are good places to start.

        As for capturing all signals you might look at sigtrap. Or you could do the following:

        $SIG{$_} = \&handler for keys %SIG;
        Note that will also call handler when you die or warn.

        --
        perl -pe "s/\b;([mnst])/'\1/mg"

Re: nohup & PERL.
by Dragonfly (Priest) on Apr 17, 2002 at 01:05 UTC
    You might want to look into socket programming in Perl; it's not really that terribly difficult to get started, and there are some good examples around the Monastary. Here is a short example of networking with IO::Socket.

    use strict; use IO::Socket; my $host = 'pdp11.mydomain.net'; my $port = 6740; my $socket = new IO::Socket::INET(PeerAddr => $host, PeerPort => $port, Proto => 'tcp'); die "cannot open socket" unless ($socket); #send a custom command to the PDP-11 server my $cmd = "GIMME\n"; print $socket $cmd; # print the server's response to the gimme request while (<$socket>) { print; } close $socket;

    The reason I suggest this type of approach is that using netcat is going to limit portability for your code, if you decide to try and run it on a platform that doesn't have netcat for instance.

    Also, using spawned external child processes will likely lead to more issues such as the one you're having. It's probably a better idea to use the functionality that's already built in to Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://159675]
Approved by belg4mit
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-20 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found