Gracefully exiting CLI application

Jan Kaliszewski zuo at
Mon Jul 27 17:52:27 EDT 2009

27-07-2009 Ben Finney <ben+python at> wrote:

> David <71david at> writes:
>> I am writing a command line application, and I need to perform some
>> cleaning on exit even if the process is killed. How can I do that with
>> python?
> Write an “exit handler” function, then use ‘atexit.register’
> <URL:> to register yours for
> processing whenever the program exits.

Unfortunately neither sys.exitfunc nor function registered with
atexit.register() are called when process is killed with signal.

As I wrote, you must use signals. Though sometimes it's a good idea
to combine these two techniques (i.e. signal handlers call sys.exit(),
then sys.exitfunc/or function registered with atexit does the actual
cleaning actions).


Jan Kaliszewski (zuo) <zuo at>

More information about the Python-list mailing list