Automatic shutdown of AnyBodyCon after licence failure

Hi,

I am running an optimization in Matlab, where the objective function is based on an AnyBody simulation. So for each iteration in Matlab, I open AnyBodyCon and run the AnyBody simulation.

Sometimes the AnyBody licence can’t be validated. Because AnyBody console stays open displaying the message “Cannot validate licence ! […] Press R to retry or any key to quit.”, the only way out is to cancel the whole optimization routine.

I check the availability of our licence server just before I open the console, by checking if I can reach a folder on the machine where our network license server is located. Although the problem just happens once in some hundred iterations, it makes it hard to complete a full optimization.

My question is:
Is there a way to automatically shutdown AnyBodyCon when the licence server is unavailable? Because then I could just retry a minute later or pause the optimization until I know the licence is available. And if so, is there an error message i can catch with Matlab?

Kind regards,

Bart Koning

Hi Bart,

First of all, I suggest you write a similar e-mail to the support where you attach the license and describe the type of it. Please DO NOT post license files on the forum.

Second, you can run the console application in a separate system thread and check the runtime of this thread. If it is higher than a chosen threshold - kill it physically and try again (then you know that the simulation has not finished properly and can handle such situation in the optimization loop).

Another option is to use the same instance of AnyBodyCon.exe and pipe the arguments into it. However, I have not tried it myself yet.

Best regards,
Pavel

Hi Pavel,
I’ve solved the issue according to your second suggestion, I just put it here for completeness. What I did is the following (code in next post):
1\ I added a second study to the macro, which only runs for one time step and creates an output file called ‘CheckRun.AnyOut’. So, as soon as this file is created, I know that the first simulation I need has run without a licence failure.
2\ By using the &-sign I open the simulation in the background and Matlab continues with the script. This way I it is possible for Matlab to check on the simulation process and kill it if necessary. Instead of directly calling AnyBodyCon.exe, I call a batch file that runs AnyBodyCon on the first line and calls ‘exit’ at line 2. This means the when AnyBodyCon is finished or killed, the command window I opened from Matlab is closed automatically.
3\ (startAMS.m) I created the startAMS function which works as followed; First, it stores the PIDs (process identifiers) of AnyBodyCon processes already running (preStartPIDs). Secondly, it then starts the new AnyBodyCon.exe and finally it again stores the AnyBodyCon PIDs (postStartPIDs). By comparing the pre- and postLoad PIDs I know the PID of the AnyBodyCon.exe I just started. This PID is then output of the startAMS function.
4\ The final step is to set the time limit for the AnyBody simulation, if this exceeded I can kill the AnyBodyCon process using the PID from step 3 and restart the console after a certain time. Offcourse, if the CheckRun.AnyOut file from step 1 is created before the time limit, no restart is necessary.
It includes some precautions, such as preventing to kill other instances of the AnyBodyCon.exe. I must say I haven’t tested it fully, yet. But it should work.
Kind regards,
Bart Koning

runAMS.m
function [] = runAMS(TimeLimit,PauseTime,MaxTimeOuts)

%% Declaration of file to be checked
CheckFile = ‘D:[…]\CheckRun.AnyOut’;
% Delete if it exists
if exist(CheckFile,‘file’)
delete(CheckFile);
end

%% Running simulation
AnyBodyTimeOuts = 0;
AnyBodyStatus = 0;
%0 - Not started
%1 - Started
%2 - Completed
while AnyBodyStatus ~= 2;
if AnyBodyStatus == 0
currentPID = startAMS();
tic
AnyBodyStatus = 1;
elseif exist(CheckFile,‘file’)
AnyBodyStatus = 2;
elseif toc > TimeLimit
if AnyBodyTimeOuts < MaxTimeOuts
% Shutdown AnyBodyCon.exe
killString = [’!taskkill /F /PID ',currentPID];
eval(killString);
% Make startup again
AnyBodyStatus = 0;
AnyBodyTimeOuts = AnyBodyTimeOuts + 1;
% Wait
pause(PauseTime)
else
keyboard
end

end
end

startAMS.m
function [currentPID] = startAMS()
% Get info
[~,tmp] = system(‘tasklist /fi “ImageName eq AnyBodyCon.exe” /fo csv /nh ‘);
preStartInfo = textscan(tmp,’%s %s %s %s %s %s’,‘delimiter’, ‘,’);
preStartPIDs = preStartInfo{1,2};
clear tmp preStartInfo

% Run new instance AnyBodyCon on the background (&).
!AMSbatch.bat &

% Get info
[~,tmp] = system(‘tasklist /fi “ImageName eq AnyBodyCon.exe” /fo csv /nh ‘);
postStartInfo = textscan(tmp,’%s %s %s %s %s %s’,‘delimiter’, ‘,’);
postStartPIDs = postStartInfo{1,2};
clear tmp postStartInfo

% check for new instances
newInst = 1;
for inst2 = 1:size(postStartPIDs,1)
% Set the current PID as new
thisPID = postStartPIDs{inst2};
isNew = 1;
% Check if it really is a new
for inst1 = 1:size(preStartPIDs,1)
% Search all
if strcmp(thisPID,preStartPIDs{inst1})
% Not a new instance of cmd, so isNew = 0!
isNew = 0;
end
end
if isNew == 1
% It is a new PID
newPID{newInst} = thisPID;
end
end

% Only one new instance found?
if size(newPID) > 1
% Unclear which is correct PID
keyboard
else
currentPID = newPID{1};
end

Hi Bart,

This is great to hear. Thank you very much for posting the solution here - other users may benefit from it.

Best regards,
Pavel