Slightly off topic: determining at the Windows system level when my process is done
2 views (last 30 days)
Show older comments
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 Comments
Answers (3)
Geoff
on 13 Jun 2012
Why don't you invoke the shutdown as the last thing your MatLab script does?
4 Comments
Richard Crozier
on 14 Jun 2012
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).
Jason Ross
on 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 Comments
Jason Ross
on 14 Jun 2012
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 :)
Image Analyst
on 13 Feb 2013
Edited: Image Analyst
on 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 Comments
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!