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

I posted this problem here yesterday. I got a response, but when I got back to work to try it, id didn't work. I'm running an external command to get some data.
open ( DUMP, "/path/to/prog argument=value |") || die $!
The problem is, prog will not send me back to a prompt unless I type 'quit'. I tried the following:
my $pid = open ( DUMP, "/path/to/prog argument=value |") || die $! while ( <DUMP> ) { do something } kill 2, $pid;
I even tried closing the file handle, but basically the script won't continue until the pid is killed (externally as it were). Any other way around this? Fork? Something I've traditionally stayed away from, so not exactly sure if that's what I need.

Thanks

Replies are listed 'Best First'.
Re: more problems running a program that hogs the termnial
by Forsaken (Friar) on Apr 15, 2005 at 07:54 UTC
    looks to me like it isn't an issue of killing the process, but instead a matter of using the <FH> style of reading that basically blocks, waiting for more input from the program. Could you elaborate a bit on what kind of program it is and what kind of data it is feeding the script? If you know for example that it will always generate x bytes or y lines of information, perhaps it would be better to perform a non-buffered read using sysread on DUMP and then closing the filehandle(assuming the program you're using won't choke on that kind of thing).

    Alternatively, is there any way to convince the program you're calling to write the information to ie a textfile, which your script can then process in a more relaxed fashion?

    Remember rule one...
Re: more problems running a program that hogs the termnial
by BrowserUk (Patriarch) on Apr 15, 2005 at 04:56 UTC

    Sounds like you could be running MS telnet?

    Did you try kill 1, $pid or kill 9, $pid;?

    I know that on Win32, most programs will terminate if they receive a SIGINT (2), but MS telnet doesn't. However, it will terminate on recieving signals 1 or 9.

    If you're not on win32, or using telnet this may not apply.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.
    Rule 1 has a caveat! -- Who broke the cabal?
      I'm running this script on Solaris 9. I tried kill 1 and kill 9 -- neither work. The script does absolutely nothing (including trying to kill it) untill the pid is killed. Hence, my delima. :(

        'k. Sorry for the bum steer. Strange though, if you can terminate the same program with ^C?

        You probably need to look at IPC::Open2 or maybe the Expect program?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco.
        Rule 1 has a caveat! -- Who broke the cabal?
        I tried kill 1 and kill 9 -- neither work.
        Then something's wrong. Signal 9 is SIGKILL. When a program is issued a SIGKILL, it must exit. The operating system ensures that. Nothing can trap a SIGKILL. Something tells me that you're not sending a signal to the correct process. How are you trying to issue the kill?

        thor

        Feel the white light, the light within
        Be your own disciple, fan the sparks of will
        For all of us waiting, your kingdom will come

Re: more problems running a program that hogs the termnial
by starbolin (Hermit) on Apr 15, 2005 at 15:01 UTC

    Your Perl code creates Unidirectional communications. You need to create a way to talk to the Remote program. See: perlipc::Bidirectional Communication. Which makes the case for using a Module.

    Note also that interrupting the input stream may require special handling. See: perlipc::Safe Signals look down under "Interrupting I/O".


    s/interupt/interrupt/


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
      Sorry for the late reply everyone. I came home last night and crashed... had a long one today. :( I tried prepending "quit |" to the command, but that isn't working so well. It runs the program, but without the proper arguments. Not sure why, but maybe I could goof around with it.

      Anyways, there are some good ideas here. I will play around a bit more after the weekend and try again. The other option is to recompile the program and not have it loop (waiting for more input). I have the source, but I'm about as good as C as I am at administering dental work.
Re: more problems running a program that hogs the termnial
by Keystroke (Scribe) on Apr 15, 2005 at 14:24 UTC
    Have you tried this?
    my $pid = open ( DUMP, "echo quit | /path/to/prog argument=value |") | +| die $!