Handling KILL signal in a perl script

Discussion in 'Perl Misc' started by Swarup Baran, Jul 25, 2006.

  1. Swarup Baran

    Swarup Baran Guest

    Hello All,

    Here is what the scenario is...

    I have a perl script which runs for hours reading data and processing
    it.

    What i need to handle is if this perl process gets a kill process
    either because of a manual kill or machine reboot etc. I should be
    able to trap it, clean up if any resources and then EXIT.

    Is it possible to do so? An urgent reply would be appreciated.

    Thanks & Regards,
    Swarup
     
    Swarup Baran, Jul 25, 2006
    #1
    1. Advertising

  2. Swarup Baran

    David Squire Guest

    Swarup Baran wrote:
    > Hello All,
    >
    > Here is what the scenario is...
    >
    > I have a perl script which runs for hours reading data and processing
    > it.
    >
    > What i need to handle is if this perl process gets a kill process
    > either because of a manual kill or machine reboot etc. I should be
    > able to trap it, clean up if any resources and then EXIT.
    >
    > Is it possible to do so?


    Yes. See perldoc perlvar, and look for $SIG.

    > An urgent reply would be appreciated.


    That is no way to talk to a newsgroup. When you are paying us you can
    tell is it's urgent.


    DS
     
    David Squire, Jul 25, 2006
    #2
    1. Advertising

  3. Swarup Baran

    -berlin.de Guest

    Swarup Baran <> wrote in comp.lang.perl.misc:
    > Hello All,
    >
    > Here is what the scenario is...
    >
    > I have a perl script which runs for hours reading data and processing
    > it.
    >
    > What i need to handle is if this perl process gets a kill process
    > either because of a manual kill or machine reboot etc. I should be
    > able to trap it, clean up if any resources and then EXIT.


    Look up %SIG in perlvar.

    > Is it possible to do so? An urgent reply would be appreciated.


    "Urgent" and "Usenet" don't go together.

    Anno
     
    -berlin.de, Jul 25, 2006
    #3
  4. Swarup Baran

    Paul Lalli Guest

    Swarup Baran wrote:
    > What i need to handle is if this perl process gets a kill process
    > either because of a manual kill or machine reboot etc. I should be
    > able to trap it, clean up if any resources and then EXIT.


    My understanding is that SIGKILL is very specifically *not* trappable.
    Otherwise, it would be possible to write a program that simply cannot
    be stopped (short of rebooting the machine). To wit:

    $ perl -e'$SIG{$_} = sub { print "Caught SIG$_[0]\n" } for keys %SIG; 1
    while 1' &
    [1] 18338
    $ kill -l
    1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
    5) SIGTRAP 6) SIGABRT 7) SIGEMT 8) SIGFPE
    9) SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS
    13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGUSR1
    17) SIGUSR2 18) SIGCHLD 19) SIGPWR 20) SIGWINCH
    21) SIGURG 22) SIGIO 23) SIGSTOP 24) SIGTSTP
    25) SIGCONT 26) SIGTTIN 27) SIGTTOU 28) SIGVTALRM
    29) SIGPROF 30) SIGXCPU 31) SIGXFSZ 32) SIGWAITING
    33) SIGLWP 34) SIGFREEZE 35) SIGTHAW 36) SIGCANCEL
    37) SIGLOST
    $ kill -1 18338
    Caught SIGHUP
    $ kill -2 18338
    Caught SIGINT
    $ kill -3 18338
    Caught SIGQUIT
    $ kill -4 18338
    Caught SIGILL
    $ kill -9 18338
    [1]+ Killed perl -e'$SIG{$_} = sub { print "Caught SIG$_[0]\n"
    } for keys %SIG; 1 while 1'
    $ ps -u plalli
    PID TTY TIME CMD
    17896 pts/4 0:00 bash
    $

    > Is it possible to do so? An urgent reply would be appreciated.


    That's just plain rude. If you need urgency, you hire a consultant.
    If you want free help, you accept it whenever someone feels like giving
    it to you.

    Paul Lalli
     
    Paul Lalli, Jul 25, 2006
    #4
  5. Swarup Baran

    Dr.Ruud Guest

    -berlin.de schreef:

    > "Urgent" and "Usenet" don't go together.


    But they can!

    Isn't there a Perl-sponsor-site where people can Pal-Pay there
    donations?
    http://donate.perl-foundation.org/

    Poster should mention the URL of the confirmed payment, to speed up the
    answers.
    ;)

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jul 25, 2006
    #5
  6. "Paul Lalli" <> wrote in news:1153835306.539633.129170
    @h48g2000cwc.googlegroups.com:

    > Swarup Baran wrote:
    >> What i need to handle is if this perl process gets a kill process
    >> either because of a manual kill or machine reboot etc. I should be
    >> able to trap it, clean up if any resources and then EXIT.

    >
    > My understanding is that SIGKILL is very specifically *not* trappable.
    > Otherwise, it would be possible to write a program that simply cannot
    > be stopped (short of rebooting the machine).


    IIRC, On the *nix machines I have run, issuing the reboot command from the
    prompt, results in the OS sending a SIGHUP, followed by a KILL. Presumably,
    the SIGHUP is to allow processes to clean up.

    I must admit, I am rather ignorant in these areas.

    Sinan
     
    A. Sinan Unur, Jul 25, 2006
    #6
  7. "Dr.Ruud" <> wrote in news:ea5evk.1h8.1
    @news.isolution.nl:

    > -berlin.de schreef:
    >
    >> "Urgent" and "Usenet" don't go together.

    >
    > But they can!
    >
    > Isn't there a Perl-sponsor-site where people can Pal-Pay there
    > donations?
    > http://donate.perl-foundation.org/
    >
    > Poster should mention the URL of the confirmed payment, to speed up the
    > answers.
    > ;)
    >


    As an economist, I very much like that idea.

    There would have to be a disclaimer of warranty specifically crafted for
    this purpose, to reflect the fact that the donation really is just a
    donation, not a fee-for-service. The donation would provide us with a
    motivation to help the donor as quickly as we can if we know the answer.

    Sinan
     
    A. Sinan Unur, Jul 25, 2006
    #7
  8. Swarup Baran

    -berlin.de Guest

    A. Sinan Unur <> wrote in comp.lang.perl.misc:
    > "Paul Lalli" <> wrote in news:1153835306.539633.129170
    > @h48g2000cwc.googlegroups.com:
    >
    > > Swarup Baran wrote:
    > >> What i need to handle is if this perl process gets a kill process
    > >> either because of a manual kill or machine reboot etc. I should be
    > >> able to trap it, clean up if any resources and then EXIT.

    > >
    > > My understanding is that SIGKILL is very specifically *not* trappable.
    > > Otherwise, it would be possible to write a program that simply cannot
    > > be stopped (short of rebooting the machine).

    >
    > IIRC, On the *nix machines I have run, issuing the reboot command from the
    > prompt, results in the OS sending a SIGHUP, followed by a KILL. Presumably,
    > the SIGHUP is to allow processes to clean up.


    SIGHUP isn't universal, I think, but the procedure is: First send
    a catchable signal and wait a bit, then SIGKILL all processes that
    haven't got the hint.

    Anno
     
    -berlin.de, Jul 25, 2006
    #8
  9. Swarup Baran

    Guest

    wrote:
    > At 2006-07-25 09:48AM, Paul Lalli <> wrote:
    > > Swarup Baran wrote:
    > > > What i need to handle is if this perl process gets a kill process
    > > > either because of a manual kill or machine reboot etc. I should be
    > > > able to trap it, clean up if any resources and then EXIT.

    > >
    > > My understanding is that SIGKILL is very specifically *not* trappable.

    >
    > Indeed. My Camel book says: "Some signals can be neither trapped nor
    > ignored, such as the KILL and STOP signals."


    I don't think the OP was considering SIGKILL specifically, but rather using
    the generic term for any signal generated by (Perl or unix) kill commands,
    which by default in fact or not SIGKILL. He would probably want to trap
    HUP, INT, and QUIT, and TERM.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jul 25, 2006
    #9
  10. On Tue, 25 Jul 2006 14:11:09 +0000, A. Sinan Unur wrote:
    > "Paul Lalli" <> wrote in news:1153835306.539633.129170
    > @h48g2000cwc.googlegroups.com:
    >> Swarup Baran wrote:
    >>> What i need to handle is if this perl process gets a kill process
    >>> either because of a manual kill or machine reboot etc. I should be
    >>> able to trap it, clean up if any resources and then EXIT.

    >>
    >> My understanding is that SIGKILL is very specifically *not* trappable.
    >> Otherwise, it would be possible to write a program that simply cannot
    >> be stopped (short of rebooting the machine).

    >
    > IIRC, On the *nix machines I have run, issuing the reboot command from the
    > prompt, results in the OS sending a SIGHUP, followed by a KILL. Presumably,
    > the SIGHUP is to allow processes to clean up.


    I think you mean SIGTERM. SIGHUP is usually used to tell daemons to
    reload their configuration.

    hp


    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
     
    Peter J. Holzer, Jul 25, 2006
    #10
  11. Swarup Baran

    Ben Morrow Guest

    Quoth -berlin.de:
    > A. Sinan Unur <> wrote in comp.lang.perl.misc:
    > > "Paul Lalli" <> wrote in news:1153835306.539633.129170
    > > @h48g2000cwc.googlegroups.com:
    > >
    > > > Swarup Baran wrote:
    > > >> What i need to handle is if this perl process gets a kill process
    > > >> either because of a manual kill or machine reboot etc. I should be
    > > >> able to trap it, clean up if any resources and then EXIT.
    > > >
    > > > My understanding is that SIGKILL is very specifically *not* trappable.
    > > > Otherwise, it would be possible to write a program that simply cannot
    > > > be stopped (short of rebooting the machine).

    > >
    > > IIRC, On the *nix machines I have run, issuing the reboot command from the
    > > prompt, results in the OS sending a SIGHUP, followed by a KILL. Presumably,
    > > the SIGHUP is to allow processes to clean up.

    >
    > SIGHUP isn't universal, I think, but the procedure is: First send
    > a catchable signal and wait a bit, then SIGKILL all processes that
    > haven't got the hint.


    This is way OT, but any user processes will receive SIGHUP when their
    session leader terminates. This is in POSIX. Process started from
    /etc/rc (or whatever) will be stopped by the same mechanism, which
    generally ends up sending a SIGTERM, though it may do other things. At
    least linux sends TERM to all processes just before halt, before a final
    KILL.

    Ben

    --
    Outside of a dog, a book is a man's best friend.
    Inside of a dog, it's too dark to read.
    Groucho Marx
     
    Ben Morrow, Jul 25, 2006
    #11
  12. Swarup Baran wrote:
    > Hello All,
    >
    > Here is what the scenario is...
    >
    > I have a perl script which runs for hours reading data and processing
    > it.
    >
    > What i need to handle is if this perl process gets a kill process
    > either because of a manual kill or machine reboot etc. I should be
    > able to trap it, clean up if any resources and then EXIT.
    >


    Normally then, someone will send you a TERM (and possibly follow that
    with an untrappable KILL if ignored), so you could catch TERM at least:

    $SIG{ TERM } = sub { ... # cleanup };


    hth,
    --
    Charles DeRykus
     
    Charles DeRykus, Jul 26, 2006
    #12
  13. A. Sinan Unur wrote:
    > "Paul Lalli" <> wrote in news:1153835306.539633.129170
    > @h48g2000cwc.googlegroups.com:
    >
    >
    >>Swarup Baran wrote:
    >>
    >>>What i need to handle is if this perl process gets a kill process
    >>>either because of a manual kill or machine reboot etc. I should be
    >>>able to trap it, clean up if any resources and then EXIT.

    >>
    >>My understanding is that SIGKILL is very specifically *not* trappable.
    >>Otherwise, it would be possible to write a program that simply cannot
    >>be stopped (short of rebooting the machine).

    >
    >
    > IIRC, On the *nix machines I have run, issuing the reboot command from the
    > prompt, results in the OS sending a SIGHUP, followed by a KILL. Presumably,
    > the SIGHUP is to allow processes to clean up.


    issuing the "reboot" command sends a message to the "init" process to
    enter runlevel 6. Init then takes over and works through the shutdown
    scripts, one of which tries to stop applications in a leveled approach,
    as you describe it.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Jul 26, 2006
    #13
    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,814
  2. Ahmed Moustafa

    How to send a kill signal from Eclipse?

    Ahmed Moustafa, Sep 2, 2003, in forum: Java
    Replies:
    4
    Views:
    14,113
    Ahmed Moustafa
    Sep 3, 2003
  3. Miguel

    signal for kill -9

    Miguel, May 26, 2006, in forum: C++
    Replies:
    5
    Views:
    583
    Tomás
    May 26, 2006
  4. sai krishna

    trap jvm kill signal

    sai krishna, Jun 5, 2007, in forum: Java
    Replies:
    3
    Views:
    962
    sai krishna
    Jun 7, 2007
  5. Peter
    Replies:
    34
    Views:
    1,942
    James Kanze
    Oct 17, 2009
Loading...

Share This Page