From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

CMD.EXE Output Not Showing up


@jqu wrote:

Long story short, the only feasible way I found to achieve the desired behavior is this:

Run System exec.vi with "cmd /c start batchfile.bat"

[...]
EDIT: Ok, I found another drawback: LabView's CPU usage goes through the roof with this, even when the batchfile isn't acutally doing anything (e.g. "pause" command). Can anyone maybe explain what is happening here?


It is just guesswork, but it seems like LabVIEW starts eating CPU because of the way it checks for when the application is finished executing. When your command is simply to spawn another instance of command prompt (i.e. "start") and exit it will break this check.

 

Reading through "start /?", "/WAIT" key caught my attention.

 

WAIT        Start application and wait for it to terminate.

 

 

This will keep the original "start" command alive all the way until the command or batch script finish execution.

You can see this difference by comparing command prompt behavior in "start" vs "start /wait".

 

And magically it solves the CPU utilization issue with system exec.vi with "cmd /c start".

 

See attached test vi and a snapshot. 

Note that you need to set "run minimized?" to true. Otherwise you will end up with two prompts - one from "cmd /c" and one from "start".

Note that it works with perfectly fine with batch files and with long commands (for example, copy or xcopy mentioned above) directly, bypassing the temporary file creation.

 

Hope this further helped with "sparing mankind some pain" :).

 

Note that this way we loose the "standard output" and "standard error" from system exec.vi, as "start" will not forward this to us.

Can be easily corrected by redirecting the stdout and stderr to a temporary file and upon completion reading from it.

 

 

Download All
Message 11 of 13
(643 Views)

@mikhail.musienko wrote:

Note that this way we loose the "standard output" and "standard error" from system exec.vi, as "start" will not forward this to us.

Can be easily corrected by redirecting the stdout and stderr to a temporary file and upon completion reading from it.


Turned out it is not that straightforward as I thought initially. If we use stdout redirection then there is no output printed on the cmd window, which defeats the whole purpose of this code. 

Here is an updated version that do both real-time print in the cmd window and saving stdout for future processing/logging/etc. Inspired by https://stackoverflow.com/a/22081167

 

 

0 Kudos
Message 12 of 13
(622 Views)

And one last thing - getting an error code from that long command back to LabVIEW.

This gets way too convoluted for multiple commands or batch files, so I limited myself with single commands (psftp in my case), as it is likely the most useful use-case.

Inspired by https://stackoverflow.com/a/38548950.

0 Kudos
Message 13 of 13
(615 Views)