MATLAB: How to stop a “system()” call after prefixed CPU time amount

elapsed timesystem

Hi, I would like to stop the execution of a simulation operated by an external function called in matlab by command "system()" when exceeding a maximum amount of CPU elapsed time, is it possible with matlab? Thank you

Best Answer

You would have to have added "&" to the end of the command line in order to have the process generated and the command to return immediately afterwards. You would not be able to get the output of the run, so it would have to be something that wrote to files.
With the background process going, to stop the process after a particular amount of CPU time, you would need to use your operating system task monitoring facilities in order to locate the process identifier. With the process identifier, you would use operating system tools figure out how much CPU time the process had used. When it used too much, you would use operating system facilities to kill the process. For OS-X and Linux, you would use the "kill" command to terminate a process; for MS Windows you would use taskkill
There is a different approach that can be used with OS-X and Linux. If someone on the command line that you system, before the actual command is started, you use ulimit -t you can limit the amount of CPU time allowed for the commands that follow it. For example,
result = system('ulimit -t 120; ./run_my_simulation');