Is there any algorithm to restart a program?


I want to have a functionality of restarting my rust program.
For an abstract example, imagine a user who write “restart” in the irc chat, some bot reads it and restarts itself. How can I do that in rust?


I usually do this with a script. The program returns a special exit code that the scripts detects and restarts the application as opposed to exiting.


Well, if you don’t have any mutable global state, you could just write:

fn main() {
  while run() {}
fn run() -> bool {
  // ... return true to restart, false to exit.

Alternatively, if you only care about supporting Linux (I mean, who cares about other OSs :smile:) , you can just exec /proc/self/exe:

fn restart() {
    use std::os::unix::process::CommandExt;
    Command::new("/proc/self/exe").exec().expect("failed to restart process");


Your solutions are very nice! :))
And also, we may add arguments to /proc/self/exe by arguments from /proc/self/cmdline!


This requires something like supervisord but I dont really want that. But thanks for posting, I’ll keep this in mind, there is a probability in near future that I can change my mind and use supervisord.


As an alternative, if you are using systemd, you can write a simple unit file: