how to kill a process from Matlab

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 Comments

Jan
Jan on 10 Oct 2021
Edited: Jan on 11 Oct 2021
A bold guess: What about:
invoke(oDesktop, 'Exit');
What happens, if you call this in a command window:
taskkill /F IM ansysedt.exe
no luck. it says it couldn't find the process
How about killing the process by PID
is there a way in Matlab to get the list of processes running in Windows 10?
Jan
Jan on 11 Oct 2021
Edited: Jan on 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.
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');

Sign in to comment.

 Accepted Answer

Bruno Luong
Bruno Luong on 11 Oct 2021
Edited: Bruno Luong on 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 Comments

Interesting, I'll try your code
@NAFTALI HERSCOVICI, so I assume you've tried it by now, so did it work?

Sign in to comment.

More Answers (4)

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 Comments

tried no luck. see my answer above
thanks
Image Analyst
Image Analyst on 10 Oct 2021
Edited: Image Analyst on 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.

Sign in to comment.

ansysedt.exe

1 Comment

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

Sign in to comment.

23 Comments

I find it hard to believe
system('taskkill /F /IM ansysedt.EXE'); % With 2 "k"s this time.
did not work. Type that in to the command line in MATLAB, capture another screenshot of both MATLAB and the taks manager processes and post both back here. Do you see any error messages anywhere? If MATLAB can't kill it, then type cmd to get a DOS Console window and type
taskkill /F /IM ansysedt.EXE
at the DOS prompt and see if that gets rid of it.
did that and no, it did not work. I suspect it's a company policy issue. I might not have the authoroty to do that.
Is there a way to check that?
I don't have that program in my computer. I don't even know what "Ansys's HFSS" is.
Is it possible that it's a special security program that your company won't let you shut down? Like an antivirus, anti-malware program?
yeah this what I am thinking
@NAFTALI HERSCOVICI: If you have the power to start the application, you are allowed to stop it also.
@NAFTALI HERSCOVICI, I think Jan's right.
So what happens if you just click on it and type the "End Task" button.
Does the process go away? If that kills it then you should be able to do it from the command line using taskkill command in the
yes it does go away
I needed in matlab because I am coding an optimization and the app needs to be open and closed at each iteration
Have you tried
taskkill /F /IM ansysedt.EXE
under command line with administrator privilege?
If it works (and not working without administrator), then you should perhaps launch MATLAB as administrator, the run your program.
yes I did. no luck
If it doesn't kill with command line with administrator privilege, there is no point for you to try under MATLAB. It's the problem of user account/OS privilege that prevents you to kill a task IMO.
but in task manager I can kill it. Doesn't this require the same of privileges?
Bruno Luong
Bruno Luong on 11 Oct 2021
Edited: Bruno Luong on 11 Oct 2021
Possibly not, but I'm not expert of Windows. All I know is all the files have separate Security config for System/User/Administrators. Perhaps TaskManager runs system security privilege.
You could also tried to take a look and change Security permission of AnsysEdt.exe
Can you answer these questions:
  1. How was this process started in the first place? (Manually, or on boot up?)
  2. Who/what started it? (You, some other program, the operating system?)
  3. Why do you need to kill it?
  4. What happens if you just let it run while your MATLAB program runs? (Probably related to #3).
Worst case, you can have MATLAB just check if the process is running and then pop up a message asking the user to shut it down somehow (task Manager or otherwise).
Bruno Luong
Bruno Luong on 11 Oct 2021
Edited: Bruno Luong on 11 Oct 2021
@Image Analyst I'm not NAFTALI HERSCOVICI but I could Imagine (I have done the same thing in the pass) he use Ansys to run many FEM simulations and uses MATLAB to optimize some design parameters, thus restart / shutdown ansys within MATLAB (using dos statement) for each requested simulation.
that is the case
Bruno Luong
Bruno Luong on 11 Oct 2021
Edited: Bruno Luong on 11 Oct 2021
Have you seen this tips in the doc file
[The system command uses the same credentials as the credentials used to start MATLAB. To set credentials for program myprogram.exe to require admin privileges, type:
system('cmd /C myprogram.exe');]
PS: this tip should be applied for "taskkill", and possibly you also need to run MATLAB with administrator right.
I still don't know what it is and still don't know the answers to any of the 4 questions I asked.
  • How was this process started in the first place? (Manually, or on boot up?)
Under MATLAB (dos/system command)
  • Who/what started it? (You, some other program, the operating system?)
within MATLAB
  • Why do you need to kill it?
Because it takes memory to have persistent program that finishes its job.
  • What happens if you just let it run while your MATLAB program runs? (Probably related to #3)
Not a good workaround, since the termination needs to be done automatically at each optimization iteration.
Thanks Bruno. Do I have this program? Is it built into MATLAB? Or one of its toolboxes? Or is it some third party, non-Mathworks program? How can I replicate this problem on my/our computer?
@NAFTALI HERSCOVICI, do you get an "out of memory error". If so, call the function
memory
at strategic places in your program to see how much memory is left. Use clear() to clear variables that are no longer needed, which will free up memory. Or try to reuse the same variable over and over again instead of creating new variables each time. Like use tempImage repeatedly instead of creating image1, image2, image3, etc.
No you don't have it : Ansys is a FEM simulation program, completely independent of MATLAB. https://www.ansys.com/
The memory is taken because it's a complex program and MATLAB cannot control memory of programs run under system().
Thanks Bruno. Perhaps it can't be killed because it's waiting for a user input. Like when you try to restart Windows and you have made a change Excel and windows says "This program is preventing you from shutting down", then if you go to Excel and take care of things there (saving or whatever), then it will let you shut down, even automatically killing the process if it has to.
Bruno Luong
Bruno Luong on 11 Oct 2021
Edited: Bruno Luong on 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).
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?

Sign in to comment.

kunal gokhe
kunal gokhe on 15 Oct 2021
Use Ctrl+C to kill Porcess.

2 Comments

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.
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

Sign in to comment.

Categories

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!