1

I would like to print the exit code in a function registered by atexit.

import atexit, sys, os @atexit.register def goodbye(): print('Exit code', sys.???) 

My script loads some not-standard modules which may raise an exception or call exit() and in all cases I would like to write the exit code to a log file. I checked the sys and os modules, but I found no such attribute of method.

UPDATE

The issue is more complicated than I thought. For quit(), exit() and sys_exit() atexit function is called. Actually they can be called practically with anything! Their argument can be empty, number, text or any class, object and anything else and this value is in the args argument of an SystemExit exception. But not for os._exit(), os.abort(). They behaves more like the C exit() and abort(), but should not be used as the housekeeping activities would be skipped!

2
  • 1
    check this question, it may be helpful. Commented Feb 12, 2022 at 19:48
  • @GiorgiImerlishvili Thanks. I have seen this solution, but it is a bit more complex, then it supposed to be. In Perl it is like END { print "Exit code: $?\n";}. It is a bit strange to me at the atexit callback I can not even read the SystemExit exception, which was thrown as sys.exc_info() returns (None, None, None). Commented Feb 13, 2022 at 12:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.