diff --git a/ports/unix/main.c b/ports/unix/main.c
index cd2dc49a52788c0d355cf9b0cb4c0a6d860622b2..004d581bb22ef9fe6eaf7a72061583f53f0dd4f7 100644
--- a/ports/unix/main.c
+++ b/ports/unix/main.c
@@ -642,6 +642,12 @@ MP_NOINLINE int main_(int argc, char **argv) {
         }
     }
 
+    #if MICROPY_PY_SYS_ATEXIT
+    if (mp_obj_is_callable(MP_STATE_VM(sys_exitfunc))) {
+        mp_call_function_0(MP_STATE_VM(sys_exitfunc));
+    }
+    #endif
+
     #if MICROPY_PY_MICROPYTHON_MEM_INFO
     if (mp_verbose_flag) {
         mp_micropython_mem_info(0, NULL);
diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h
index 97a9f4908415a32114d480737b4944f8961c81bc..123cad2bc2967e1bc1cd0c3380d4e820a9460b34 100644
--- a/ports/unix/mpconfigport.h
+++ b/ports/unix/mpconfigport.h
@@ -90,6 +90,7 @@
 #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
 #define MICROPY_PY_BUILTINS_SLICE_ATTRS (1)
 #define MICROPY_PY_SYS_EXIT         (1)
+#define MICROPY_PY_SYS_ATEXIT       (1)
 #if defined(__APPLE__) && defined(__MACH__)
     #define MICROPY_PY_SYS_PLATFORM  "darwin"
 #else