Kill a system process if it takes too long

41 views (last 30 days)
H R on 2 Apr 2021
Commented: H R on 6 Apr 2021
I use a system command to run an external program PERR that takes the input file + some other arguments to run. After the matlab program reaches to this system call, the matlab program runs PERR and waits for PERR to finish. Then I read the output file that is generated by PERR and continue my code. I repeat this operation multiple times within a for loop. However, sometimes after calling the system command, this program (PERR) takes a long time to finish. I would like to kill this program if the execution time of this system run takes more than 5 minutes and continue the rest of my code .
The program is called using system on Matlab (MS Windows 10) using the following code
YourCommand = sprintf('"%s" --piece=mpi --np=3 "%s"', cmd1, name);
I have serached the web (taskkil or timer) and could not find any solution for this. I appreciate it if someone could help solve this issue. For example I used this:
YourCommand = sprintf('"%s" --piece=mpi --np=3 "%s"', cmd1, name);
t = timer;
t.TimerFcn = @(~,~)system(YourCommand);
t.StopFcn = @(~,~)system('kill ');
t.Period =300;
but none of them worked

Answers (1)

per isakson
per isakson on 2 Apr 2021
Edited: per isakson on 2 Apr 2021
I did this long time ago and have forgotten the details. And I haven't access to my code now. Thus from the top of my head. There are more than one way to do it.
PERR does it run as one or more processes? Or a series of processes invoking each other? The latter initially caused me a headache.
My first solution was based on taskkill(). The problem was to pick up the process id, PID. I had several copies of the external program running and needed the PID to distinguish between them. I retrieved a list of running processes (using tasklist()), started the program (using system()), paused a second or two, and retrieved a new list and run a diff. Not very elegant or robust. See Walter's comment in kill a processb from matlab.
My final solution was based on .NET. It worked nicely. See How can I kill a system process which is frozen and Managing Processes in .NET.
I believe PowerShell provide powerful commands, but I never got my head around it. And maybe it's worth looking at Getting Started with Microsoft .NET- Examples and concepts to help you quickly get started using .NET in MATLAB
per isakson
per isakson on 4 Apr 2021
Edited: per isakson on 4 Apr 2021
Try Sysinternal's PsKill. The documentation says:
Parameter Description
-t Kill the process and its descendants.
Remember that in addition to killing the processes there might be files, which shall be closed.
H R on 6 Apr 2021
Thank you per. Yes I have seen this, but did not work in my case. I think I am missing something there. I'll post if I can get this work

Sign in to comment.


Find more on Introduction to Installation and Licensing 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!