Kill a system process if it takes too long

26 views (last 30 days)
H R
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 DATA.data + 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
cmd1='PERR';
name='DATA.data'
YourCommand = sprintf('"%s" --piece=mpi --np=3 "%s"', cmd1, name);
system(DATA.data)
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:
cmd1='PERR';
name='DATA.data'
YourCommand = sprintf('"%s" --piece=mpi --np=3 "%s"', cmd1, name);
t = timer;
t.TimerFcn = @(~,~)system(YourCommand);
t.StopFcn = @(~,~)system('kill ');
t.Period =300;
t.TasksToExecute=1;
t.ExecutionMode='fixedRate';
start(t);
or
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
  6 Comments
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
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.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!