Default Timeout after inactivity

21 次查看(过去 30 天)
2000rogers
2000rogers 2013-10-14
Hi,
As part of the university, we are using network licenses from the university-wide license server. Matlab (central installation on a mounted network disk) is run by our users (~100) on a couple of (around 20) networked unix computers. While the licenses are returned automatically when users are inactive for a couple of time, the (Matlab-) programs itself are still running consuming memory and other system resources.
We're looking for an option to exit Matlab after a time of inactivity to 'clean up' our systems from unwanted resources usage. Since all users execute a matlab program of the same installation it would be optimal to be able to select a default value (e.g. 2 hrs) which could be overwritten by users individually.
Looking through Matlab preferences and help as well as through the support FAQ & questions I didn't found such an option. Did I overlook something? Otherwise this would be a feature that seems to be really helpful in coping with our limited resources but which is still missing ...
Regards, Roger

回答(4 个)

Jan
Jan 2013-10-14
编辑:Jan 2013-10-14
It is unclear what "inactivity" exactly means, when the "Matlab-programs" are still running. Do you mean the Matlab application or does Matlab run any user-defined code?
In both cases it might be brute to kill the process after a fixed period of time. But if brute methods are ok, you couldlog the users off from the terminal server after a certain delay. This would free even more resources.
A more or less dirty trick would be a timer, which obtains the contents of the command window every 5 minute and stores its fingerprint. When it did not change for 2 hours, exit the Matlab session - after showing a dialog to warn the user. To my surprise this can be achieved using 3 functions from the FEX written by the same author (shameless self-advertisment):
  1. http://www.mathworks.com/matlabcentral/fileexchange/32005-cmdwintool
  2. http://www.mathworks.com/matlabcentral/fileexchange/31272-datahash
  3. http://www.mathworks.com/matlabcentral/fileexchange/24871-autowarndlg
function QuitOnIdle
UD.Time = now;
UD.Hash = '';
T = timer('ExecutionMode', 'fixedSpacing', ...
'BusyMode', 'drop', ...
'ObjectVisibility', 'off', ... % Hide timer handle
'Period', 300, ... % 5 minutes
'TimerFcn', @checkActivity, ...
'UserData', UD, ...
'Tag', 'QuitOnIdle_Timer'); % If somebody is wondering
start(T);
function checkActivity(TimerH, EventData)
UD = get(TimerH, 'UserData');
CmdText = CmdWinTool('getText');
newHash = DataHash(CmdText);
if ~isequal(newHash, UD.Hash)
UD.Now = now;
UD.Hash = newHash;
set(TimerH, 'UserData', UD);
elseif etime(now, UD.Now) > 2 * 3600 % 2 hours without a change
Opt.Delay = 120; % Wait 2 minutes
Opt.Button = {'Shut down', 'Keep running'}; % 1st is default
Opt.Interpreter = 'tex';
Opt.Wrap = true;
[Reply, TimeOut] = AutoWarnDlg( ...
{'This Matlab session is shut down automatically after 2 hours of idle.'}, ...
'You have a chance to keep this session running.'}, ...
'Auto Shutdown', Opt);
if strcmp(Reply, 'Shut down')
try
% New Matlab versions allow to save open files in the editor programatically!
% But this should be included in TRY CATCH, because older versions do not know this.
% Close open figures:
try
close('all', 'hidden'); % close all figures
catch % DeleteFcn crashed?
Figs = findobj(allchild(0), 'flat', 'type', 'figure');
set(Figs, 'DeleteFcn', '', 'HandleVisibility', 'on');
close('all'); % close all figures
end
% Close all open files:
fclose('all');
% Save the current workspace to a MAT file in the user folder?
folder = strtok(userpath, pathsep); % First userpath folder only
file = fullfile(folder, sprintf('AutoSave_%s.mat', datestr(now, 0)));
evalin('base', ['save(''', file, ''')]);
catch
end
% Delete this timer?!
% Goodbye:
quit('force');
else
UD.Now = now; % Start a new period
set(TimerH, 'UserData', UD);
end
end
Now add a call to this function inside startup.m, or if this is user-defined add it with admin privileges in matlabrc.m.
Test this exhaustively! This has been written from scratch in the forum's editor, so neither MLint nor runtime checks have been applied!
It kills Matlab sessions and the users will be very angry if they loose any data. Let all users sign on paper (not any checkbox dialog, which is ignored by all adult computer users), that they know and accept this restriction. Check what happens, if teh system clock is reset at the change of the daylight saving time or if the user resets the clock. And again:
Killing sessions is potentially evil! Do not blame me in case of troubles.
[EDITED] I forgot to mention a thrilling method to catch the user's attention: Let the machine beep massively before the closing dialog appears.
See also for saving currently used data and files:
  1. http://www.mathworks.com/matlabcentral/fileexchange/37486-matlab-state
  2. http://www.mathworks.com/matlabcentral/fileexchange/34577-savecurrenteditorfiles

Andreas Goser
Andreas Goser 2013-10-14
It seems to me the root cause is that end users do not care about correctly closing those sessions. And I would assume that those users not necessarily have set up smart ways of saving their data and that killing such a session would probably have impact on their work. So even if the behaviour of those users is to blame, I would not recommend killing sessions (Note that I see also Jan saying this in a similar way).
Saying that, I would not address this technically... I am sure you can somehow identify the users' login name / email address and send them kind emails where you describe the impact of what they are doing.
  1 个评论
Jan
Jan 2013-10-14
My suggested function could be used to send a message to the admin containing the user and computer name instead of the killing.
I hesitate to elaborate the Auto-Killer and publish it in the FEX. But if I do so, the default behavior will be an annoying warning dialog only asking the user to shutdown manually.

请先登录,再进行评论。


2000rogers
2000rogers 2013-10-14
Hi,
thnx to Your responses. Indeed the root cause of our problem is the 'laziness' of our users. I'm doubtful about the practical value of solutions which identify 'lazy' users and send them e-mails.
To clarify 'inactivity':
The previous command has finished without error condition and the Matlab prompt is visible ...
Our users typically running scripts were data is processed (and the results are stored), but don't 'exit' Matlab at the end of their processing (since it is convenient to don't have to start the session again).
We need a simple to apply solution which works for all users and don't causes additional administration work. A 'brute force' timeout could be deactivated by default. Therefore I cannot understand why such a simple mechanism is not implemented in standard Matlab.
Roger
  4 个评论
Jan
Jan 2013-10-14
@2000rogers: But you find such a simple mechanism implemented in Matlab on this page. It requires a few lines of code only, but has a high risk of deleting data. This is a good reason not to ship this in a standard installation.
It is impossible to detect, if the previous command has finished without an error condition, except if you allow a well define set of function for the users only. Even then a core dumping crash will not set the error condition sufficiently. The visibility of the Matlab prompt is a weak condition also: The prompt can be active while a GUI is open, whose UserData contain valuable results.
2000rogers
2000rogers 2013-10-17
Session Timeouts are quite common when it comes to security ...
@Andreas Goser: Session timeout examples for other programs are underway ...
@Jan Simon: * a simple solution would be the mentioned variable, with a safe default * errors during code execution should be detected by the interpreter * coding could be done in a way as to omit such ambiguous program states

请先登录,再进行评论。


Melanie Guthrey
Melanie Guthrey 2017-1-3
编辑:Melanie Guthrey 2017-1-3
Hello Roger, I don't know if you are still having this issue, but you can now use FlexNet Manager to set a TIMEOUT for MATLAB. See more info here: https://www.mathworks.com/matlabcentral/answers/98614-can-i-configure-my-license-server-to-return-idle-matlab-seats Good luck!

类别

Help CenterFile Exchange 中查找有关 Manage Products 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by