Function self_replace::self_replace

source ยท
pub fn self_replace<P: AsRef<Path>>(new_executable: P) -> Result<(), Error>
Expand description

Replaces the running executable with a different one.

This replaces the binary with another binary. The provided path is copied over and if the function successfully completes, you can delete the source binary.

use std::fs;

let new_binary = "/path/to/new/binary";
self_replace::self_replace(&new_binary)?;
fs::remove_file(&new_binary)?;

Note that after this function concludes, the new executable is already placed at the old location, and the previous executable has been moved to a temporary alternative location. This also means that if you want to manipulate that file further (for instance to change the permissions) you can do so.

By default the permissions of the original file are restored.