Documentation

exepid

use std::{env, fs::write, path::Path, process};

pub fn exepid() -> std::io::Result<()> {
  let pid = process::id();

  let pid_file = if let Ok(fp) = env::var("PID_FILE") {
    fp.into()
  } else {
    let exe_path = env::current_exe()?;
    let exe_folder = exe_path.parent().unwrap_or_else(|| Path::new("."));

    let program_name = exe_path
      .file_stem()
      .and_then(|os_str| os_str.to_str())
      .unwrap_or("");

    exe_folder.join(format!("{}.pid", program_name))
  };

  write(pid_file, pid.to_string())?;

  Ok(())
}

About

This project is an open-source component of i18n.site ⋅ Internationalization Solution.

关于

本项目为 i18n.site ⋅ 国际化解决方案 的开源组件。