reload_self 0.1.20

Cross-platform process hot reload library / 跨平台进程热重载库
Documentation
use std::{os::unix::process::CommandExt, process::Command};

use nix::unistd;
use tokio::signal::unix::{SignalKind, signal};

/// Wait for SIGHUP signal / 等待 SIGHUP 信号
pub async fn wait_reload() {
  let mut stream = signal(SignalKind::hangup()).expect("Failed to create SIGHUP signal stream");
  stream.recv().await;
}

/// Setup Unix process detachment / 设置 Unix 进程分离
pub fn setup_process_detachment(command: &mut Command) {
  unsafe {
    command.pre_exec(|| {
      // Create new session, completely detach from controlling terminal and parent process
      // 创建新的会话,完全脱离控制终端和父进程
      unistd::setsid().map_err(std::io::Error::other)?;
      
      // Notify systemd about the main PID on Linux / 在 Linux 上通知 systemd 主进程 PID
      #[cfg(target_os = "linux")]
      {
        use sd_notify::NotifyState;
        let _ = sd_notify::notify(
          false,
          &[NotifyState::MainPid(std::process::id())],
        );
      }
      
      Ok(())
    });
  }
}