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

hi, I have a scenario that I find it weird and might need to solve it by sending a ctrl+c through open3 function.

my perl script is run from windows, which will call plink (a command based putty to connect to Linux) to run Linux commands. All is fine if the process for Linux finishes and then gets back to windows.

now, my scenario is that while it is processing in plink, someone tries to do a control+c to break the running perl script abruptly.

it ends up windows command prompt has no processes running, but the Linux server connected from plink still thinks the process is not terminated. So I was wondering if I can detect a control+c is happening while perl script is running , then send the contrl+c command to open3's to plink before I terminate.

hi, is there anywhere I can send a ctrl+c to open3 before I exit my script?

Replies are listed 'Best First'.
Re: send ctrl+c to open3?
by AppleFritter (Vicar) on Jul 18, 2014 at 09:36 UTC
    My first instinct is that plink isn't the best way to go about this and that it'd be better to use a standard module (perhaps Net::SSH::Perl) to log in to your Linux box instead. Then, proceed as zentara suggested: trap SIGINT, and perform the appropriate cleanup in the signal handler routine, closing the connection gracefully etc.
Re: send ctrl+c to open3?
by zentara (Cardinal) on Jul 18, 2014 at 09:12 UTC
    I'm not sure about the CNTRL-C being the best or easiest way to stop the process being run by open3. IPC::Open3 returns a PID, so what you might want to try is intercept the CNTRL-C in the parent, then use Proc::Killfam to killfam 9, $pid;

    The killfam is used to get all shells and children related to the $pid in question. Often, the $pid returned by open3, is just a shell used to run your actual program.

    Here is how to grab cntrl-c in your main.

    # intercept CNTRL-C , kill open3, exit $SIG{INT} = sub { killfam 9,$pid; exit(0) };
    You can google for many examples of using Proc::Killfam, I won't repeat them here.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: send ctrl+c to open3?
by David92 (Sexton) on Jul 18, 2014 at 08:56 UTC

    Let me se if I understand correclty:

    You are running PERL scripts on Windows, through some client that connects you to Linux. And now you want to use a CTRL+C command in WINDOWS to terminate the current ran PERL script. Yet CTR+C is also a command that terminates anything in Command Prompts / lines and it terminates your Windows Command , but does not terminate the PERL's script. Correct?