Reputation: 135
I'm trying to execute command docker run --name test ubuntu
, but the program will end in line 4 when this command run.
I want to continue program after executed command. How can I solve this problem?
code:
let argument = format!("docker run --name test ubuntu");
let mut cmd = std::process::Command::new("sh");
let cmd = cmd.arg("-c").arg(argument);
// the program will end here
assert!(cmd.status().unwrap().success());
Upvotes: 2
Views: 3000
Reputation: 23244
I'm not sure what you mean when you say "the program will end here". However, from your code, the program should block on the assert
line until the command finishes. How else could you get the status you're asking for, since it is only available when the command exits?
If you want your program to keep running in parallel with the command you started, you should use spawn
instead of status
:
let argument = format!("docker run --name test ubuntu");
let mut cmd = std::process::Command::new("sh");
let cmd = cmd.arg("-c").arg(argument);
// the program will end here
let child = cmd.spawn().unwrap();
That way your program keeps executing. If you need it, you can later wait for the command to finish and get its exit status then with child.wait()
(as the name implies, wait
will block your program waiting for the child to finish) or child.try_wait()
(which will return None
if the child has not yet exited and its status is therefore not available).
Upvotes: 5