MATLAB Answers

Slightly off topic: determining at the Windows system level when my process is done

David Goldsmith 님이 질문을 제출함. 13 Jun 2012
Hi! In Windows7, I have a long-running process running in the command interpreter, i.e., once it's done, the OS will still see the MATLAB.exe process as running. I want to shutdown my computer when the process is done, but I don't want to sit here waiting indefinitely for that to happen. I'd like to write a little DOS batch script that will detect when the process is done and then excecute the shutdown command--any help would be greatly appreciated. Thanks!

  댓글 수: 0

로그인 to comment.

답변 수: 3

Geoff 님의 답변 13 Jun 2012

Why don't you invoke the shutdown as the last thing your MatLab script does?

  댓글 수: 4

표시 이전 댓글 수: 1
Okay, I don't quite follow you, because the question says "I want to shutdown my computer when the process is done". Are you saying that the "process" is something other than a MatLab program and that it runs non-modal (invoked from MatLab, program running, but control returned to user)?
Now, if it's just a MatLab script you can pass an optional flag to your function that requests it to shutdown afterwards. Do the usual 'nargin' test, and if it wasn't passed, assume that a shutdown is not required. I do this with automated MatLab processes (except I just want to quit, not shut down) so that I can still run tests in the workspace without the frustration of having it quit on me.
If you really do need to monitor another process, that can be achieved by polling the process list via the Windows API. I needed to do it at my old job to prevent starting a program again after crash (because the OS would allow huge memory allocation and then bluescreen before the crashed image resources were freed). I don't have the code anymore of course, but I don't mind knocking something up for you in C... maybe if you wanted to paypal a beer or something =)
OK, I have MATLAB running; at its command prompt, I've typed in "my_script", which proceeds to run. Now, my_script takes a long time--hours. I want to go home before it's done, but (I didn't notice 'til after I'd started my_script) my computer is complaining about needing to restart to install updates, which also takes a long time, so I'd like that to also happen while I'm away. So, I'd like the computer to shutdown/restart, but not untill my_script--which is already running--is done.
I was hoping to use something like the DOS command TASKLIST to monitor MATLAB's CPU activity and when it went to zero-ish, issue the SHUTDOWN command, but the closest thing TASKLIST seems to offer is memory usage, and I'm not sure how much memory MATLAB will still be using when the process is done (I have a number of large globals that I neglect to routinely clear at the end of the script), so I don't see this as a very robust alternative.
Clear-er?
I still don't see from this why you can't just put this at the end of your matlab script:
system('shutdown')
or
system('shutdown -r')
to restart.
When the script's done, matlab tells the computer to shutdown. This is what David Goldsmith wants you to do. You can use the system command (or the bang '!' operater to have matlab issue commands to the OS it's running on).

로그인 to comment.


Jason Ross 님의 답변 14 Jun 2012

It's going to be very difficult to detect "done-ness" programmatically if the process is already started. Some variation of Geoff's suggestions would work, or if you wanted to keep the shutdown logic separate from your script, you could exit MATLAB as the last part of the script and then gate your shutdown logic on the presence of the matlab.exe process. Trying to gate it on cpu usage or memory utilization becomes a guessing game with the potential for false positives or stuff not triggering correctly (and especially too early). The process not existing is a very clear-cut yes or no signal that means all the work is done for sure, all files are closed and results are done.
To detect matlab.exe, you can use switches in the tasklist program ("tasklist /?" lists them)

  댓글 수: 2

OK, I can see that, somehow, what I was wanting to do isn't getting through (or it's just not possible and no one wants to simply say "sorry, it can't be done.") In any event, it's long since moot, so now I just know for the future: before I run my_script, check to see if I'm going to need to shut my computer down when it's done, and plan accordingly.
I'll say "It can't be done*".
* - easily, with a simple batch script. The interface to detect the CPU load isn't readily accessible, and checking that could leave you open to false positives when the process isn't quite "done" and you essentially pull the rug out under the process when you shut down the machine.
To make it work in your situation, you'd need to
- monitor the CPU load of until it went to zero for some time
- identify the PID
- kill that PID (and hope a dialog didn't pop up saying "are you sure?")
- then issue the shutdown command
That's a lot of "maybes" to put into something you throw together in a few minutes :)

로그인 to comment.


Image Analyst 님의 답변 13 Feb 2013
Image Analyst 님이 편집함. 13 Feb 2013

David:
You could try something like this. It calls task list to see if your app is in the list. Then, if you want you can put it into a loop where it keeps monitoring it until you shut it down and then it alerts you that it has shutdown.
% find_running_process.m
% Finds out if a process is running.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Execute the system command
% tasklist /FI "IMAGENAME eq yourApp.exe"
% First define the name of the program we're looking for.
% You can run it then execute "tasklist" in a
% CMD console window if you don't know the exact name.
taskToLookFor = 'yourApp.exe';
% Now make up the command line with the proper argument
% that will find only the process we are looking for.
commandLine = sprintf('tasklist /FI "IMAGENAME eq %s"', taskToLookFor)
% Now execute that command line and accept the result into "result".
[status result] = system(commandLine)
% Look for our program's name in the result variable.
itIsRunning = strfind(lower(result), lower(taskToLookFor))
if itIsRunning
message = sprintf('%s is running.', taskToLookFor);
else
message = sprintf('%s is not running.', taskToLookFor);
end
uiwait(helpdlg(message));
message = sprintf('Do you want to monitor it until it finishes?');
button = questdlg(message, 'Wait for shutdown?', 'Yes', 'No', 'Yes');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'No')
return;
end
% Go into a loop waiting for it to finish.
maxChecks = 10; % Max seconds to wait before exiting - a failsafe.
numberOfChecks = 1;
while itIsRunning && numberOfChecks < maxChecks
% Now execute that command line and accept the result into "result".
[status result] = system(commandLine);
% Look for our program's name in the result variable.
itIsRunning = strfind(lower(result), lower(taskToLookFor));
if itIsRunning
message = sprintf('%s is still running after %d seconds.\n',...
taskToLookFor, numberOfChecks);
fprintf('%s', message);
else
message = sprintf('%s is not running anymore.\n', taskToLookFor);
fprintf('%s', message);
uiwait(helpdlg(message));
break; % Exit loop.
end
pause(1); % Wait a second before checking again.
numberOfChecks = numberOfChecks + 1;
end
msgbox('Done with demo!');

  댓글 수: 0

로그인 to comment.



Translated by