Kill a system process if it takes too long
26 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (1)
per isakson
el 2 de Abr. de 2021
Editada: per isakson
el 2 de Abr. de 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 comentarios
per isakson
el 4 de Abr. de 2021
Editada: per isakson
el 4 de Abr. de 2021
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.
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!