#shutdown #callback #termination

no-std simple_on_shutdown

This crate consists of a convenient macro to specify on shutdown callbacks (=code that should run when your program exits (grafecully)). It’s super simple and stripped-down.

13 releases (1 stable)

1.0.0 May 19, 2021
0.3.0 Dec 21, 2020
0.2.2 Dec 21, 2020
0.1.8 Dec 15, 2020

#71 in No standard library

Download history 13/week @ 2021-02-20 3/week @ 2021-02-27 26/week @ 2021-03-20 28/week @ 2021-03-27 26/week @ 2021-04-03 26/week @ 2021-04-10 9/week @ 2021-04-17 5/week @ 2021-04-24 13/week @ 2021-05-01 27/week @ 2021-05-15 3/week @ 2021-05-22 2/week @ 2021-05-29 15/week @ 2021-06-05

56 downloads per month

MIT license

89 lines


This crate consists of a convenient macro to specify on shutdown callbacks called on_shutdown!. It takes code that should be executed when your program exits (gracefully). See https://docs.rs/simple_on_shutdown for more info.

Useful with "runtimes you do not have control over", like for example actix-web framework doesn't let you specify shutdown callbacks by yourself. In such cases my macro may be a better option.



use simple_on_shutdown::on_shutdown;

fn main() {
    // macro can take: direct expression
    on_shutdown!(println!("shut down with success"));
    // closure expression
    on_shutdown!(|| println!("shut down with success"));
    // move closure expression
    on_shutdown!(move || println!("shut down with success"));
    // block
    on_shutdown!({ println!("shut down with success") });
    // identifier
    let identifier = || println!("shut down with success");


See "examples/"-dir in repository!.

⚠ Restrictions ⚠

  • There is no guarantee that this gets executed in "non-regular" shutdown scenarios, like CTRL+C / SIGINT / SIGTERM
  • your application must handle SIGINT/SIGTERM (and other signals) properly to allow a gracefully "non-regular" shutdown (Actix web framework does this for example)
    • i.e. if you don't handle signals CTRL+C will immediately shut down your app
  • Even in that case: there is no guarantee in every case that the operating system gives your application more time after it has been (forcefully) killed
  • this behaviour differs a little between Windows and UNIX. See example/src/bin/simple_example_ctrl_c_signal for more details.

No runtime deps