MATLAB Answers

kill an external process (simulation ) if it is taking too long

6 views (last 30 days)
Sajid Afaque
Sajid Afaque on 16 Dec 2019
Commented: Sajid Afaque on 19 Dec 2019
Hey everyone,
even i am using the same stratergy ( ) to call an external function and pause for several minutes then kill the process.
it is fine when my external process get into some infinite numerical calculations or it gets strucks.
but my problem is it waits for "n" amount of time even for process that finishes early that n.
can any1 suggest any approach where it waits and kills only when the process gets struck.
it should not wait for the good ones.


Rik on 16 Dec 2019
Is it possible for your external program to create a flag file? Then you could loop a short pause and a check for the existence of the file.

Sign in to comment.

Answers (2)

Guillaume on 18 Dec 2019
You could use a timer for this, it would be something like:
killtimer = timer('ExecutionMode', 'singleshot', 'StartDelay', 5, 'TimerFcn', @killtask); %executes after 5 seconds
start(killtimer); %start the timer of your code as usual
and a seperate function
function killtask(~, ~)
try %wrap in a try catch so that if the program is already terminated we don't throw an error


Sajid Afaque
Sajid Afaque on 19 Dec 2019
actually i am calling the external function in a loop
while 1
% code to call external process
if ~exist('components.txt') || timeout
% exit and kill the process if active
so when i call external function it reads component.txt and generate txt files on it own.thwn it comes back to matlab and processes further.
now , sometimes when i call the external process it runs into infinite i need to implement a timeout mechanism as shown above ,such that if the time exceeds the defined time it kills the external process and comes out of while the same time if the external process works well ,i dont want to wait for the defined time(since the process is complete ,i will exit from the loop)
Guillaume on 19 Dec 2019
I don't understand why people ask a question, get answers that solve the very question they ask, then come back with "actually..." and different question. Why didn't you ask that in the first place?
If you want to interrupt a loop after a certain time simply put a time limit as part of the loop condition, something like:
tstart = tic;
while toc(start) < yourdelay
Sajid Afaque
Sajid Afaque on 19 Dec 2019
sorry for that Guillaume , but its a training for new guys like us.
we will get more exposed to the concepts.
w.r.t above loop, I tried even that,but one problem which i found when using that code was
1)imagine a condition ,where my external process is running and by mistake or due to some reason that tab(external process) get that case if above loop is implemented , i have to re-run the entire code
2)but when "while 1" loop is implemented,even if it get cancelled it will re-run ,but again here i face the initial problem(

Sign in to comment.

Walter Roberson
Walter Roberson on 18 Dec 2019
If you are using MS Windows then you can use .NET as described in . You could loop checking the status at short interals.
If I recall correctly, I have read that you can create callbacks for .NET code; if that is correct, then perhaps it would be possible to create a callback when the process finishes normally; you could use that together with a timer() so as to kill the process if it runs too long.


Show 2 older comments
Walter Roberson
Walter Roberson on 19 Dec 2019
Yes, well try not doing that. Use the .NET interface to create the process and start it. You can loop querying the process status, and you can break the loop if it finishes, and if you reach the end of the for loop without having finished the process then use the NET interface to kill the process.
Or potentially instead of looping checking you might be able to configure a NET event to notify you on termination. With that and a timer object for the case where you timeout, you could potentially avoid having to loop checking status. The looping version is probably easier to write.
Sajid Afaque
Sajid Afaque on 19 Dec 2019
thanks Walter,
i am partially understanding , as i am new and not yet comfortable with these terms.
can you please write and show me a demo code ? it would be a favour
Walter Roberson
Walter Roberson on 19 Dec 2019
See the link I already posted as it shows creating the process.
I do not have a Windows MATLAB installed to test with so I cannot write complete code myself.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!