9
submitted 1 month ago* (last edited 1 month ago) by [email protected] to c/[email protected]

So, apparently the chrome/geckodriver processes will terminate on their own if the user sends ctrl+c to the console. It will not terminate on its own if the program finishes running naturally.

If you're interested in terminating it on your own, like I also was, here is how I went about it.

use std::process::{Child, Command};

fn main() {
	let mut s = Server::default();
	s.start();
	s.shutdown();
}

struct Server {
	child: Option<Child>
}

impl Default for Server {
	fn default() -> Self {
		Self {
			child: None
		}
	}
}

impl Server {
	fn start(&mut self) {
		self.child = Some(Command::new("./chromedriver")
			.spawn()
			.expect("ls command failed to start"));
	}

	fn shutdown(&mut self) {
		input(None); // wait for input so you can observe the process
		self.child.as_mut().unwrap().kill();
		self.child.as_mut().unwrap().wait();
		println!("shutdown");
	}
}

pub fn input(prompt: Option<String>) {
	let mut input = String::new();
	match prompt {
		Some(prompt) => println!("{}", prompt),
		None => ()
	}
	io::stdin().read_line(&mut input).expect("Failed to read input");
}
top 1 comments
sorted by: hot top controversial new old
[-] [email protected] 4 points 1 month ago

I have no specific knowledge of this stuff. But the generic answer is an obvious one.
However Python code does it can be replicated in Rust.
For example, if python code executes the driver as a subprocess, then that too can be done in Rust.
So checking how Python code does it will probably lead to an answer to this question.

this post was submitted on 08 Jun 2024
9 points (100.0% liked)

Rust Programming

7734 readers
2 users here now

founded 5 years ago
MODERATORS