use std::io::Write;
use std::process::Command;
use std::time::Duration;
fn main() {
let mut args = std::env::args().skip(1);
let ms: u64 = args.next().and_then(|a| a.parse().ok()).unwrap_or(60_000);
let mut sentinel: Option<String> = None;
let mut is_inner = false;
for arg in args {
match arg.as_str() {
"--role=inner" => is_inner = true,
s if s.starts_with("--sentinel=") => {
sentinel = Some(s.trim_start_matches("--sentinel=").to_string());
}
_ => {}
}
}
if is_inner {
std::thread::sleep(Duration::from_millis(ms));
return;
}
let me = std::env::current_exe().expect("current_exe");
let mut child = Command::new(&me)
.arg(ms.to_string())
.arg("--role=inner")
.spawn()
.expect("spawn grandchild");
if let Some(path) = sentinel
&& let Ok(mut f) = std::fs::File::create(&path)
{
let _ = writeln!(f, "{}", child.id());
}
let _ = child.wait();
}