Kill a system process if it takes too long

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

Respuestas (1)

per isakson
per isakson el 2 de Abr. de 2021
Editada: per isakson el 2 de Abr. de 2021

1 voto

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 comentarios

H R
H R el 2 de Abr. de 2021
Thank you per.
I have to run an optimization of a problem whose engine is PERR. I use GA and each iteration has 40 simulations using PERR. I am running 40 instances of PERR in parallel using matlab prallel processing i.e. each matlab worker is allocated to one instance of the PERR using the system command.
H R
H R el 2 de Abr. de 2021
I tried the .NET solution, however, I am runing into another problem; the process calls another child processes. When I use the process.Kill() the program still runs because of the child processes. I could not find a way to kill all the parent and child processes at the same time.
per isakson
per isakson el 3 de Abr. de 2021
Editada: per isakson el 4 de Abr. de 2021
No use I speculate, but
  • This is a Windows question, rather than a Matlab one. I found Sysinternals useful. Maybe, it's better to ask at a Windows forum.
  • Doesn't Process have a child process property - or something?
I googled "windows kill child process". How come this simple question generates so many hits and no simple answer? I guess the reason is that
  • the developer of PERR and many other developers forgot to set the “kill children on close/terminate” flag.
  • the designers of Windows missed this use case.
H R
H R el 4 de Abr. de 2021
Than you per for the links. I am trying various things to see if I can figure this out.
I think matlab can easily add this functionality to the "system" command. Something like a "timeout" keyword.
per isakson
per isakson el 4 de Abr. de 2021
Editada: per isakson el 4 de Abr. de 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 el 6 de Abr. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Parallel Computing Toolbox en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

H R
el 2 de Abr. de 2021

Comentada:

H R
el 6 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by