atexit/signal for non-interactive jobs

K

kj

I want to implement clean-up functions for scripts to be run on a
Linux cluster (through LSF). The goal is to make sure that a
minimal wrap-up sequence (print diagnostic info, flush buffers,
etc.) gets executed if the job is terminated for some reason. (The
most common reason for premature termination is a SIGUSR2 signal,
sent to the process by LSF when the job has taken longer than the
time limit for the job's LSF queue.)

No matter what I try, I can't get this wrap-up sequence to work
when the script runs non-interactively.

The latest I have looks something like this:

import signal
import atexit

def set_handlers(handler):
atexit.register(handler)
for sig in dir(signal):
if sig.startswith('SIG') and not '_' in sig:
signal(getattr(signal, sig), handler)

def wrapup(*args):
# etc, etc

def main():
# ...
set_handlers(wrapup)
run()

main()


If I start the script interactively and after a few seconds (i.e.
before it terminates) I hit Ctrl-C (which sends a TERM signal to
the process), the wrapup function gets called as desired (although
this action appears to be triggered by atexit, and not by the
handler for SIGTERM, which apparently never gets called).

But if instead I background the script after starting it, and then
use kill to explicitly send a TERM signal to its process, the script
terminates, but the wrapup code does not execute.

It makes no sense to me. If someone cares to explain it, I'd be
very grateful. (Is my code doing something wrong?)

But more to the point, what must I do to get this to work in the
non-interactive case?

TIA!

~k
 
C

Cameron Simpson

| If I start the script interactively and after a few seconds (i.e.
| before it terminates) I hit Ctrl-C (which sends a TERM signal to
| the process), the wrapup function gets called as desired (although
| this action appears to be triggered by atexit, and not by the
| handler for SIGTERM, which apparently never gets called).

I hate to tell you this, but ^C sends SIGINT, not SIGTERM.
--
Cameron Simpson <[email protected]> DoD#743
http://www.cskk.ezoshosting.com/cs/

To have no errors
Would be life without meaning
No struggle, no joy
- Haiku Error Messages http://www.salonmagazine.com/21st/chal/1998/02/10chal2.html
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top