Terminate the entire Perl script when control_C in an application

K

Kuhl

Hi, all:

I wrote a Perl script. It invokes an application at some steps. If the
application is running or is waiting in the license queue, then
Control_C would terminate the application. But it does not terminate
the Perl script. The script continues to run following steps. However,
such behavior causes errors finally. So I want to terminate not only
the application when I do Control_C, but want to terminate the entire
Perl script. I wish that the script continues only if the application
finishes in the normal way. How to achieve this goal?

Thank you in advance.
 
J

Josef Moellers

Kuhl said:
Hi, all:

I wrote a Perl script. It invokes an application at some steps. If the
application is running or is waiting in the license queue, then
Control_C would terminate the application. But it does not terminate
the Perl script. The script continues to run following steps. However,
such behavior causes errors finally. So I want to terminate not only
the application when I do Control_C, but want to terminate the entire
Perl script. I wish that the script continues only if the application
finishes in the normal way. How to achieve this goal?

Thank you in advance.

This is probably OS dependent, as on Linux, upon a Ctrl-C a SIGINT is
sent to all processes that have the current terminal as their
controlling tty. Thus the application *and* the Perl script will get killed.

This may, however, be different on other OSes.
 
B

Ben Morrow

How are you invoking the program? Using system? If so, you can check $?
to see if the program exitted on SIGINT, using the WIFSIGNALED and
WTERMSIG functions from the POSIX module.
This is probably OS dependent, as on Linux, upon a Ctrl-C a SIGINT is
sent to all processes that have the current terminal as their
controlling tty. Thus the application *and* the Perl script will get killed.

*However*, system explicitly ignores SIGINT for the duration of the
external command, so that perl isn't killed by a ^C. If you don't want
that, either fork/exec/wait yourself or check the exit status.

Ben
 
X

xhoster

Kuhl said:
Hi, all:

I wrote a Perl script. It invokes an application at some steps. If the
application is running or is waiting in the license queue, then
Control_C would terminate the application. But it does not terminate
the Perl script.

Check the exit status of the application. If it exited due to a signal
(or whatever other criterion), then have Perl exit.

system "./application" and die "application ended abnormal $! $?";

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
X

xhoster

Josef Moellers said:
This is probably OS dependent, as on Linux, upon a Ctrl-C a SIGINT is
sent to all processes that have the current terminal as their
controlling tty. Thus the application *and* the Perl script will get
killed.

The Perl script will get signaled. However, perl might arrange to ignore
those signals. That is what "system" does, anyway.

perldoc -f system

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
J

Josef Moellers

The Perl script will get signaled. However, perl might arrange to ignore
those signals. That is what "system" does, anyway.

perldoc -f system

Ah, one learns something new every day.

Thanks (to you and to Ben) for pointing out.

Josef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top