How to do graceful shutdown in rust for cli programs?

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

Our great sponsors
  • Scout APM - Less time debugging, more time building
  • SonarQube - Static code analysis for 29 languages.
  • 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.

  • Scout APM

    Less time debugging, more time building. Scout APM allows you to find and fix performance issues with no hassle. Now with error monitoring and external services monitoring, Scout is a developer's best friend when it comes to application development.

  • 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