필터 지우기
필터 지우기

Kill a system process if it takes too long

조회 수: 37 (최근 30일)
H R
H R 2021년 4월 2일
댓글: H R 2021년 4월 6일
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

답변 (1개)

per isakson
per isakson 2021년 4월 2일
편집: per isakson 2021년 4월 2일
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
per isakson
per isakson 2021년 4월 4일
편집: per isakson 2021년 4월 4일
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 2021년 4월 6일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by