I meant to write sys.addShutdownHook all along. Thanks @rolandkuhn.