Take the 2-minute tour ×

I am working on modifying a command-line program that is written in MSDN C and Pro*C (Oracle pre-compiler to write in-line SQL and PL/SQL statements) so that multiple copies can be spawned and process concurrently. It is a database-heavy program and concurrency issues are mostly taken care of on the database side, so we thought it would be easier to just run multiple copies than to alter the program to run truly multi-threaded.

Anyway, we rely on printf() and output piping to write the program's output to log files for debugging purposes. I am having trouble launching separate copies of the exe that appropriately write to their own log files. I have played a lot the exec() and system() functions to get different copies of the EXE to launch and write to logs. The closest I have gotten to work is using a C line such as:

system("start cmd /k call [program commmand and args] > log_file.txt");

This works great - spawns separate command windows and spawns separate copies of the program for the appropriate data-sets. The only problem is that the command windows stay open after the program has finished executing. Some of our clients run this program frequently on a scheduler, and having to manually close all of the command windows would not be acceptable for them.

Other similar commands that I have tried to get to what I am looking for are like:

system("[program command and args] > log_file.txt"); 


exec("[program command and args] > log_file.txt");

Both of these will execute a new copy of the program, write to the log files just fine, and close the command window when the process is finished, but the commands wait for the newly spawned EXE to finish running to return control to the launching executable. This obviously prevents multiple copies from running at the same time, which was the whole goal to begin with, so this is not a good solution either.

I also played with trying to add an "exit" command to the end of the command line windows by appending to the exiting command line, hoping that I could get the command window to close, like this:

system("start cmd /k call \"[program commmand and args] > log_file.txt; exit\""); 

to no avail. I tried some similar variations, but could never the correct behavior.

I would greatly appreciate any advice that could get the correct behavior. I am basically looking to launch multiple copies of an executable that run concurrently and write to separate log files, using the " > log_file.txt" output-piping feature of the windows command prompt. I would like to avoid having to use threading libraries (It's been awhile and I'm under time constraints) or using something other than printf() and output piping, since these print statements are used throughout the application and it would be a large effort to effectively replace all of the function calls at this point in time.

Anyone know a way to get the command windows to close using system() calls, or have some other easy method of solving the problem? Keep in mind that there are some time constraints involved, so I'm not necessarily looking for the best way to do this, but the quickest and easiest. I will have the opportunity to implement proper logging functionality soon, but I need to get past this problem first.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You said your first solution works great except it doesn't close the command window after the program is done executing.


The /K option states Carries out the command specified by string but remains

The /C option states Carries out the command specified by string and then terminates

You should be able to change your original solution to use the /C option as so

system("start cmd /C call [program commmand and args] > log_file.txt");
share|improve this answer
add comment

Your Answer


