Something strange happend when I used process::Command to execute "cmd"

Hello, I am trying to create a child process to execute cmd.exe.
When I used process::Command to execute "cmd", the process of "cmd" was created one by one and never stopped. However, when I used process::Command to execute other exe, nothing strange happened. Is there any advice?
Thanks

Code:

std::process::Command::new("cmd").args(&["/C", "dir >> 1.txt"]).creation_flags(0x00000010).output();

It won't stop because the executable cmd is never meant to stop on it's own.

1 Like

thanks for reply.
I mean it is creating new process of cmd all the time rather than creates only one and exits like the following picture:

Is your application called cmd.exe? It could be calling itself.

Well this seems to be a Windows specific problem and unfortunately, I don't have a Windows machine at hand to test this out.
However, on reading the docs, I think this is not the proper way to launch a terminal on Windows. The docs are here.
You may want to leave out the creation_flag and try it that way.

This may also be the case if you run the program from within target/debug or target/release.

I only used the code above, and I think it is calling itself too. But I have no idea about why it is doing that. Maybe some command is passed to it?

The creation_flag is used to make me see the consoles. When I leave out it, nothing changed actually.

Right but what did you name your project when you created it? Did you do something like:

cargo new cmd

The name should be stored in your Cargo.toml file. You can try changing it.

Ah, that's the reason! The problem is solved by changing the name. (what a stupid mistake.)

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.