Clear Filters
Clear Filters

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

2 views (last 30 days)
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!

Answers (3)

Geoff
Geoff on 13 Jun 2012
Why don't you invoke the shutdown as the last thing your MatLab script does?
  4 Comments
David Goldsmith
David Goldsmith on 14 Jun 2012
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?
Richard Crozier
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).

Sign in to comment.


Jason Ross
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
David Goldsmith
David Goldsmith on 14 Jun 2012
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.
Jason Ross
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 :)

Sign in to comment.


Image Analyst
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!');

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!