I would like to execute any bash command. I found Command::new
but I'm unable to execute "complex" commands such as ls ; sleep 1; ls
. Moreover, even if I put this in a bash script, and execute it, I will only have the result at the end of the script (as it is explain in the process doc). I would like to get the result as soon as the command prints it (and to be able to read input as well) the same way we can do it in bash.
相关问题
- how to get running process information in java?
- Stop child process when parent process stops
- Share Arc between closures
- Program doesn’t terminate when using processes
- Function references: expected bound lifetime param
相关文章
- How can I convert a f64 to f32 and get the closest
- What is a good way of cleaning up after a unit tes
- What is the difference between execl and execv?
- How to start a process in its own process group?
- How can I unpack (destructure) elements from a vec
- How to import macros in Rust?
- socket() returns 0 in C client server application
- How to get struct field names in Rust? [duplicate]
Command::new
is indeed the way to go, but it is meant to execute a program.ls ; sleep 1; ls
is not a program, it's instructions for some shell. If you want to execute something like that, you would need to ask a shell to interpret that for you:To get the output, there are two ways:
output
method is blocking and returns the outputs and the exit status of the command.spawn
method is non-blocking, and returns a handle containing the child's processstdin
,stdout
andstderr
so you can communicate with the child, and await
method to wait for it to cleanly exit. Note that by default the child inherits its parent file descriptor and you might want to set up pipes instead:You should use something like: