Terminate the entire Perl script when control_C in an application

Discussion in 'Perl Misc' started by Kuhl, Sep 26, 2008.

  1. Kuhl

    Kuhl Guest

    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.
    Kuhl, Sep 26, 2008
    #1
    1. Advertising

  2. Kuhl wrote:
    > 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.

    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Sep 26, 2008
    #2
    1. Advertising

  3. Kuhl

    Ben Morrow Guest

    Quoth Josef Moellers <>:
    > Kuhl wrote:
    > >
    > > 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?


    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.

    > > 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.


    *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

    --
    BEGIN{*(=sub{$,=*)=sub{print@_};local($#,$;,$/)=@_;for(keys%{ #
    $#}){/m/&&next;**=${$#}{$_};/(\w):/&&(&(($#.$_,$;.$+,$/),next);$/==\$*&&&)($;.$
    _)}};*_=sub{for(@_){$|=(!$|||$_||&)(q) )));&((q:\:\::,q,,,\$_);$_&&&)("\n")}}}_
    $J::u::s::t, $a::n::eek:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $,
    Ben Morrow, Sep 26, 2008
    #3
  4. Kuhl

    Guest

    Kuhl <> wrote:
    > 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.
    , Sep 26, 2008
    #4
  5. Kuhl

    Guest

    Josef Moellers <> wrote:
    > Kuhl wrote:
    > > 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.


    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.
    , Sep 26, 2008
    #5
  6. Kuhl

    Kuhl Guest

    Hi, thank you everyone. It worked.
    if(!$?==0){exit 0;}
    Kuhl, Sep 27, 2008
    #6
  7. wrote:

    > 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
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Sep 29, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,757
  2. Gros Bedo
    Replies:
    2
    Views:
    1,274
    Piet van Oostrum
    Jul 10, 2008
  3. Gros Bedo
    Replies:
    1
    Views:
    646
    Piet van Oostrum
    Jul 10, 2008
  4. norseman
    Replies:
    0
    Views:
    549
    norseman
    Jul 10, 2008
  5. Gros Bedo
    Replies:
    0
    Views:
    404
    Gros Bedo
    Jul 13, 2008
Loading...

Share This Page