On Mon, 14 Feb 2000, Paul Lussier wrote:

> >Whats the best way in perl to run an external command and if while executing
> >the command I hit ctrl + c it will just cancle the external command and contin
> >ue on with the script instead of bailing out of everything?
> 
> 
> Well, that's kind of tough.  You probably want a system command to exec an 
> external command, but in order to deal with ctrl+c, you'll have to trap the 
> key sequence yourself, and deal with it elsewhere in your code.

In C, what you would need to do is have the parent program (which in this
case is your perl code) catch the SIGINT using the signal() system call so
that it does not exit, and then send the child process the SIGINT (any
number of ways to do that).

Since I understand perl can use the system calls directly (though I don't
know how to do it (yet)), all you should need to do is:

1. Save the PID of the child process.  I'm sure there's a way to do
this...

2. use signal( SIGINT, siginthandler ) to catch the SIGINT. siginthandler,
at least in C, is a function that you want called when the program catches
a SIGINT.

3. Write your function, siginthandler, to handle the sigint. In C it would
look something like

int siginthandler(){

  int status;
  char buff[100];

  /* assume PID is a global variable containing the PID of the child */
  /* there are other ways to handle this */

  /* store the command to kill the process in a buffer */
  sprintf(buff, "kill -TERM %d", PID);

  /* use the system function call to run the Unix kill command */
  status=system( buff );

  /* return the exit status of the kill command (not used) */
  return status;
}

This is a quick hack -- this code could be improved in several ways, and
may, in fact, even be wrong... :)  But it illustrates the general idea.

How to translate this into perl?  Well, that I don't know.

-- 
"Quis custodiet ipsos custodes?"    "Who watches the watchmen?" 
-Juvenal, Satires, VI, 347 

Derek D. Martin      |  Senior UNIX Systems/Network Administrator
Arris Interactive    |  A Nortel Company
[EMAIL PROTECTED]  |  [EMAIL PROTECTED]
-------------------------------------------------


**********************************************************
To unsubscribe from this list, send mail to
[EMAIL PROTECTED] with the following text in the
*body* (*not* the subject line) of the letter:
unsubscribe gnhlug
**********************************************************

Reply via email to