register_shutdown_function

register_shutdown_function(callable $callback, mixed ...$args): void

Registers a callback to run after script execution finishes (or exit() is called), fired in registration order.

C C23

C23
#include <stdio.h>
#include <stdlib.h>

static void cleanup(void) {
    printf("shutdown: cleanup\n");
}

static void audit(void) {
    printf("shutdown: audit\n");
}

int main(void) {
    /* atexit handlers run in reverse registration order (ISO C) */
    atexit(audit);
    atexit(cleanup);
    printf("body\n");
    return 0;
}
Output
body
shutdown: cleanup
shutdown: audit

C++ C++26

C++26
#include <iostream>

// A scope-guard struct: destructor (Drop-style) fires at scope end — the C++
// idiom for "work when this block exits"
struct ShutdownGuard {
    ~ShutdownGuard() { std::cout << "shutdown: guard\n"; }
};

int main() {
    ShutdownGuard guard;
    std::cout << "body\n";
    return 0;
}
Output
body
shutdown: guard

C# v11.0.100

v11.0.100
// AppDomain.ProcessExit fires when the CLR shuts down normally
// (default 2s budget for handlers; not called on SIGKILL)
AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
{
    Console.WriteLine("shutdown: ProcessExit");
};
Console.WriteLine("body");
Output
body
shutdown: ProcessExit

Go v1.27.4

v1.27.4
package main

import "fmt"

// Go defer is a per-function hook, not per-process: it fires when main
// returns. There is no process-level shutdown queue in the language.
func worker() {
	defer fmt.Println("shutdown: worker defer")
	fmt.Println("worker body")
}

func main() {
	defer fmt.Println("shutdown: main defer")
	worker()
	fmt.Println("body")
}
Output
worker body
shutdown: worker defer
body
shutdown: main defer

Java v27.0.1

v27.0.1
// JVM shutdown hook runs when the VM exits normally (SIGINT/SIGTERM ok,
// SIGKILL no); registered as a Thread that has not yet started
class Main {
    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread(() ->
            System.out.println("shutdown: hook")));
        System.out.println("body");
    }
}
Output
body
shutdown: hook

JavaScript v26.13.1

v26.13.1
// Node: process.on("exit") runs synchronously at process shutdown —
// the register_shutdown_function analogue (no async work allowed inside)
process.on("exit", (code) => {
    console.log("shutdown: exit code", code);
});
console.log("body");
Output
body
shutdown: exit code 0

PHP v8.6.0

v8.6.0
// Shutdown callbacks run after the script finishes (or exit()) —
// in registration order, before object destruction
register_shutdown_function(function (): void {
    echo "shutdown: cleanup\n";
});
register_shutdown_function(function (string $tag): void {
    echo "shutdown: {$tag}\n";
}, "audit");
echo "body\n";
Output
body
shutdown: cleanup
shutdown: audit

Python v3.15.2

v3.15.2
import atexit

# atexit runs at interpreter exit in LIFO order (reverse of registration)
def cleanup(tag):
    print(f"shutdown: {tag}")

atexit.register(cleanup, "audit")
atexit.register(lambda: print("shutdown: cleanup"))
print("body")
Output
body
shutdown: cleanup
shutdown: audit

Rust v1.99.1

v1.99.1
// Rust: Drop runs at object destruction (scope end); a guard struct gives
// a shutdown hook covering the rest of main's scope — object-level, not
// process-level (no process-wide shutdown queue in the language)
struct ShutdownGuard;

impl Drop for ShutdownGuard {
    fn drop(&mut self) {
        println!("shutdown: guard dropped");
    }
}

fn main() {
    let _guard = ShutdownGuard;
    println!("body");
} // Drop fires when main ends
Output
body
shutdown: guard dropped

TypeScript v26.13.1

v26.13.1
// TS: same runtime hook as JS — process.on("exit") is the process-level
// shutdown analogue; types only document the payload shape
const onShutdown = (code: number): void => {
    console.log("shutdown: exit code", code);
};
process.on("exit", onShutdown);
console.log("body");
Output
body
shutdown: exit code 0

Language differences

C: atexit(3) is the process-level shutdown hook (reverse registration order); it is not guaranteed under SIGKILL/SIGTERM — the same limitation as PHP.

C++: the language-level counterpart is destructors/scope guards (object-level); for process level use std::atexit (same as C).

C#: AppDomain.ProcessExit is the process-level hook (default 2s handler budget, truncated when exceeded); not called on SIGKILL.

Go: defer is per-function, not per-process — it fires when the function returns, and deferred calls still run after an unrecovered panic; there is no process-level shutdown queue (os.Exit terminates immediately, skipping defers!).

Java: Runtime.addShutdownHook is process-level, fired on normal VM exit/trusted signals; runs after System.exit too, not on SIGKILL.

JavaScript: Node's process.on("exit") is process-level and synchronous (no async work inside); browsers have no process concept (beforeunload/pagehide are the page-level counterparts).

PHP: register_shutdown_function runs in registration order and accepts extra arguments (second call in the example); objects are not yet destroyed, so callbacks can still use them; calling exit() inside one stops the remaining shutdown functions; not run on SIGKILL/SIGTERM.

Python: atexit is process-level but in reverse registration order (LIFO, opposite of PHP); skipped when the interpreter is hard-killed (SIGKILL) or via os._exit.

Rust: the Drop trait is object-level (fires at scope end, not process end); there is no process-wide shutdown queue — wrap main's lifetime in a guard struct when needed (example).

TypeScript: runtime identical to JavaScript (Node's process.on("exit")); types only annotate the payload.