How to do graceful shutdown in rust for cli programs?

This page summarizes the projects mentioned and recommended in the original post on /r/rust

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • signal-hook

    Rust library allowing to register multiple handlers for the same signal

  • If you're interested in the techniques people use here, you might take a look at signalfd - which gives you a way to read signals as-if through a file pipe (so you can use it with things like select/epoll loops) - or signal-hook's implementation of the signal-pipe pattern (which makes use of the fact that write is on the list of async-signal-safe system calls).

  • daemonize-me

    Rust library to ease the task of creating daemons

  • Hey everyone,I'm new to rust. I'm trying to create a background service using daemonize-me that will send post request every hour, when I restart my computer or run kill $(pidof xxx) I want to do some cleanup for example delete the pid file.

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • rust

    Empowering everyone to build reliable and efficient software.

  • Unfortunately I think you have to be careful using condition variables in the signal-handler context. Condvar (on linux at least) is implemented by pthread_cond_signal. Sadly, pthread_cond_signal can't be used here:

  • crossbeam

    Tools for concurrent programming in Rust

  • In fact, crossbeam's select! isn't implemented in terms of libc::select (as you'd probably guess - I did at least!). I would guess that's because crossbeam's channels aren't really files / don't have anything to do with file-descriptors. What crossbeam does instead is it parks in a loop until timeout. Like FenrirW0lf mentioned below the comment about using Condvar, park (and friends) are also not safe to call in a signal handler - but that's okay here, since signal_hook has already let us escape that context.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts