Running external program from Matlab for a given amount of time

7 views (last 30 days)
Hello all!
I've got the following problem:
My code opens an external program (using "dos" command) and makes it process a certain data file, it looks like this:
dos('<path_to_program>\program -i <path_to_input_file>\input_file &')
"&" sign allows Matlab to open multiple instances of that program at the same time, as it returns control to Matlab immediately after starting the program and the code above is nested in a for loop, in which the input_file name is changing, so that a couple of files can be processed simultaneously.
Now, my question is, is it possible to limit the time of execution of that external program? I don't have to worry about saving it's results, as they're being written to an output file on the go.
Let's say, I would like each instance of that external program to run for an hour and then just close.
I would very much appreciate help on this one.
All the best
Filip

Accepted Answer

Jason Ross
Jason Ross on 17 Jul 2012
Edited: Jason Ross on 17 Jul 2012
Probably the most straightforward way to do this would be to write a wrapper around the call to the external program. It would work something like this:
  • The wrapper should take the path to the input file and pass it on.
  • The wrapper will launch the external command (you can use cmd /c "subcommand") and determine the PID of the launched process. There are a few means out there to do this (powershell, wmi, etc)
  • The wrapper will then use something like tasklist to check the CPU time of the launched process (or you could also record the start time and then wait an hour)
  • The wrapped will call something like taskkill with the PID if the process hasn't already stopped on it's own.
  • I'd also highly recommend that this wrapper write out some basic logging.
The big thing you need to be careful of is how you track the PIDs and processes you have running. You certainly don't want to kill a process that's been running under an hour!
Other options:
  • If you have control (as in "source code") over the external program, add in logic to make it stop itself in an hour. This can be problematic, though, if it hangs and cannot terminate itself.
  • It might be possible to use the jobs and task interface in Parallel Computing Toolbox to launch the external program as a task, then periodically test the time, and end the task if it exceeds an hour.
  3 Comments
Filip Janasz
Filip Janasz on 17 Jul 2012
Edited: Walter Roberson on 26 Feb 2013
OK, I've figured out a very crude way of doing the job, but it will do for the time. The code logic works like this:
%start your external task
dos('<path_to_program>\program_name -i <path_to_input_file>\input_file &')
%wait for n seconds (defined by hand)
wait(n)
%kill the task
dos('taskkill /im <program_name.exe>')
If I start the program multiple times (e.g. first command is in a loop), then the last command kills all instances of the program simultaneously - which is OK as long as they start +/- at the same time, fortunately in this case the time of execution does not need to be very precise. As I have multiple files to process (100+) but my computer can handle +/- 5 at once, the code above will go inside another loop, that will basically start 5 tasks, kill them after some time and start another batch of 5, until files run out. It's not the most elegant I guess, but for now it will have to do.
All the best to fellow Matlabers and thanks again Jason!
Jason Ross
Jason Ross on 17 Jul 2012
No problem. As long as it works, it's good!
The solution I proposed is more of a general purpose one where you might be launching the other processes in a manner where you wanted each to have an hour of run-time. It's something that can get complicated fairly quickly, it's (literally) keeping track of a lot of child(ren) processes!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Parallel Server in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!