[host] Add graceful shutdown on SIGINT/SIGTERM (#15387)

This commit is contained in:
J. Nick Koston
2026-04-03 08:27:13 -10:00
committed by GitHub
parent 6fecd72049
commit 2a5933e4f7
+13 -1
View File
@@ -5,11 +5,17 @@
#include "esphome/core/helpers.h"
#include "preferences.h"
#include <csignal>
#include <sched.h>
#include <time.h>
#include <cmath>
#include <cstdlib>
namespace {
volatile sig_atomic_t s_signal_received = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
void signal_handler(int signal) { s_signal_received = signal; }
} // namespace
namespace esphome {
void HOT yield() { ::sched_yield(); }
@@ -72,11 +78,17 @@ uint32_t arch_get_cpu_freq_hz() { return 1000000000U; }
void setup();
void loop();
int main() {
// Install signal handlers for graceful shutdown (flushes preferences to disk)
std::signal(SIGINT, signal_handler);
std::signal(SIGTERM, signal_handler);
esphome::host::setup_preferences();
setup();
while (true) {
while (s_signal_received == 0) {
loop();
}
esphome::App.run_safe_shutdown_hooks();
return 0;
}
#endif // USE_HOST