how to kill a process from Matlab

162 vues (au cours des 30 derniers jours)
NAFTALI HERSCOVICI
NAFTALI HERSCOVICI le 10 Oct 2021
Hello,
I need to unload Ansys's HFSS from memory from Matlab.
I am able to close the project file using
invoke(oDesktop,'Closeproject',project_full_pathname');
to close the application (HFSS) I am trying
status=dos('taskill /F IM ansysedt.exe');
I am using Ansys HFSS 2021R2 and Matlab
the command is simply ignored. No error but also no execution
Any ideas?
Thanks
  4 commentaires
Jan
Jan le 11 Oct 2021
Modifié(e) : Jan le 11 Oct 2021
"it says it couldn't find the process" - please post a copy of the message, not a rough paraphrasation. I have suggested two methods. To which one does the description "couldn't find process" belong?
How do you start the process?
I assume, this is still a communication problem. Some important details are missing, so please try to explain what you do as exact as possible.
Sander
Sander le 15 Oct 2021
For this specific HFSS case, an easier solution than the current accepted answer might be to use the QuitApplication function in the oDesktop object.
invoke(oDesktop,'QuitApplication');

Connectez-vous pour commenter.

Réponse acceptée

Bruno Luong
Bruno Luong le 11 Oct 2021
Modifié(e) : Bruno Luong le 12 Oct 2021
I don't know if it is matter but I always use PID to kill a process, not directly the image name. Here is my code
function KillSrvProcess(PrgName)
% KillSrvProcess(PrgName)
% Kill a process of program PrgName
% INPUT:
% PrgName is a char array, the name of the program to be killed
if nargin < 1
PrgName = 'LSAlgoSrv';
end
if ~contains(PrgName, '.exe','IgnoreCase', true)
PrgName = sprintf('%s.exe', PrgName);
end
Mypid = GetCurrentProcessId();
fprintf('Current pid == %d\n', Mypid);
ntries = 30;
for n = 1:ntries
cmd = sprintf('TaskList -fi "ImageName eq %s"', PrgName);
[status, out] = system(cmd);
if status > 0
warndlg('Need administrator privilege?', 'KillSrvProcess');
return
end
lines = strsplit(out,newline());
idxc = regexp(lines, ['^' PrgName], 'start', 'once');
keep = ~cellfun('isempty', idxc);
lines = lines(keep);
pidstr = regexp(lines, sprintf('%s\\s+([0-9]+)', PrgName), 'tokens', 'once');
pid = cellfun(@str2double, pidstr);
% Remove the Current Process ID
pid(pid == Mypid) = [];
if isempty(pid)
break
end
for k = 1:length(pid)
% 'cmd /C ...' To set credentials for taskkill to require admin privileges
cmd = sprintf('cmd /C taskkill /f /pid %d', pid(k));
status = system(cmd);
if status > 0
warndlg('Need administrator privilege?', 'KillSrvProcess');
return
end
end
pause(1);
end
end % KillSrvProcess
You migh comment the lines GetCurrentProcessId() and call
KillSrvProcess('ansysedt')
This is the GetCurrentProcessId() mex file
// mex -O GetCurrentProcessId.c
#include <string.h>
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
plhs[0] = mxCreateDoubleScalar(GetCurrentProcessId());
}
  2 commentaires
NAFTALI HERSCOVICI
NAFTALI HERSCOVICI le 11 Oct 2021
Interesting, I'll try your code
Image Analyst
Image Analyst le 11 Oct 2021
@NAFTALI HERSCOVICI, so I assume you've tried it by now, so did it work?

Connectez-vous pour commenter.

Plus de réponses (4)

Image Analyst
Image Analyst le 10 Oct 2021
I'm not sure there is a taskill (with a single "k"). You should try taskkill instead. Here's an example to forcibly shut down Excel
system('taskkill /F /IM EXCEL.EXE');
  2 commentaires
NAFTALI HERSCOVICI
NAFTALI HERSCOVICI le 10 Oct 2021
tried no luck. see my answer above
thanks
Image Analyst
Image Analyst le 10 Oct 2021
Modifié(e) : Image Analyst le 10 Oct 2021
What processes do you see if you type Control-shift-Esc?
Attach a screenshot of the window showing as many processes as will fit.

Connectez-vous pour commenter.


NAFTALI HERSCOVICI
NAFTALI HERSCOVICI le 10 Oct 2021
ansysedt.exe
  1 commentaire
Image Analyst
Image Analyst le 10 Oct 2021
Modifié(e) : Image Analyst le 10 Oct 2021
What is that? Do you care to explain at all?
You said you tried
system('taskkill /F /IM ansysedt.EXE'); % With 2 "k"s this time.
and it didn't work

Connectez-vous pour commenter.


NAFTALI HERSCOVICI
NAFTALI HERSCOVICI le 10 Oct 2021
  23 commentaires
Bruno Luong
Bruno Luong le 11 Oct 2021
Modifié(e) : Bruno Luong le 11 Oct 2021
Normally the option "/f" of taskkill would force the process to be killed even it is not in iddle state (busy doing something else).
Image Analyst
Image Analyst le 11 Oct 2021
Yeah, you'd think. So I'm not 100% trusting she's doing it the right way. @NAFTALI HERSCOVICI, can you run Windows's PSR (type it in after you hit the start button) and record your screenshots as you try to kill this process from MATLAB? Then save the recording (it will go into a zip file) and attach the zip file here with the paperclip icon so we can see exactly what you're doing?

Connectez-vous pour commenter.


kunal gokhe
kunal gokhe le 15 Oct 2021
Use Ctrl+C to kill Porcess.
  2 commentaires
Image Analyst
Image Analyst le 15 Oct 2021
That will only kill the current MATLAB script running. It won't kill a totally different program such as ansysedt.exe or Excel or Word or any other program. It doesn't even kill MATLAB.
NAFTALI HERSCOVICI
NAFTALI HERSCOVICI le 15 Oct 2021
I need to kill HFFSS from Matlab. I don't think this would do it. Definitely not interested killing the script. it's in a loop that must continue

Connectez-vous pour commenter.

Catégories

En savoir plus sur Manage Products dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by