Detect if user has a valid MatLab license

We're writing a shell script that we want to have it check if the user has a valid MATLAB license. If they do, the script will launch a MATLAB function call from the command line, otherwise it will call an equivalent (but slower) numpy script.
On most of our users' Linux machines, they have their MATLAB license stored under /home/<user>/.matlab/R2021a_licenses. These cases are easy to deal with. However, on some of the machines, the user licenses were installed under /usr/local/MATLAB/R2021a/licenses. On those machines, licenses for multiple users are stored under that directory. For example, we might have a machine where users foo and bar have valid license files, but user baz does not. If I look in /usr/local/MATLAB/R2021a/licenses I might see the following files:
license_MyHost_123456_R2021a.lic, license_MyHost_987654_R2021a.lic
I've looked at the contents of the license file and there doesn't seem to be a human-readable username listed in there, just the license number. So when users foo, bar, and baz each run my bash shell script, is there anything I can key off to determine that users foo and bar do have a license so my script should use the MATLAB path, and baz does not have a MATLAB license, so they get the numpy version? Ideally maybe knowing which of the two license files goes with which user might also be helpful too - identifying that user foo specifically has license 123456, might be helpful too since then I could maybe offer them the option of copying that license file down into /home/foo/.matlab/R2021a_licenses

6 Comments

This might be helpful.
ver
----------------------------------------------------------------------------------------------------- MATLAB Version: 9.13.0.2145394 (R2022b) Update 3 MATLAB License Number: 0 Operating System: Linux 5.4.224-0504224-generic #202211101403 SMP Thu Nov 10 21:10:29 UTC 2022 x86_64 Java Version: Java 1.8.0_292-b10 with AdoptOpenJDK OpenJDK 64-Bit Server VM mixed mode ----------------------------------------------------------------------------------------------------- MATLAB Version 9.13 (R2022b) Simulink Version 10.6 (R2022b) 5G Toolbox Version 2.5 (R2022b) Aerospace Blockset Version 5.3 (R2022b) Aerospace Toolbox Version 4.3 (R2022b) Antenna Toolbox Version 5.3 (R2022b) Audio Toolbox Version 3.3 (R2022b) Automated Driving Toolbox Version 3.6 (R2022b) Bioinformatics Toolbox Version 4.16.1 (R2022b) Bluetooth Toolbox Version 1.1 (R2022b) Communications Toolbox Version 7.8 (R2022b) Computer Vision Toolbox Version 10.3 (R2022b) Control System Toolbox Version 10.12 (R2022b) Curve Fitting Toolbox Version 3.8 (R2022b) DO Qualification Kit Version 3.14 (R2022b) DSP System Toolbox Version 9.15 (R2022b) Database Toolbox Version 10.4 (R2022b) Datafeed Toolbox Version 6.3 (R2022b) Deep Learning Toolbox Version 14.5 (R2022b) Econometrics Toolbox Version 6.1 (R2022b) Embedded Coder Version 7.9 (R2022b) Filter Design HDL Coder Version 3.1.12 (R2022b) Financial Instruments Toolbox Version 3.5 (R2022b) Financial Toolbox Version 6.4 (R2022b) Fixed-Point Designer Version 7.5 (R2022b) Fuzzy Logic Toolbox Version 3.0 (R2022b) Global Optimization Toolbox Version 4.8 (R2022b) HDL Coder Version 4.0 (R2022b) HDL Verifier Version 7.0 (R2022b) IEC Certification Kit Version 3.20 (R2022b) Image Acquisition Toolbox Version 6.7 (R2022b) Image Processing Toolbox Version 11.6 (R2022b) Industrial Communication Toolbox Version 6.1 (R2022b) Instrument Control Toolbox Version 4.7 (R2022b) LTE Toolbox Version 3.8 (R2022b) MATLAB Compiler Version 8.5 (R2022b) MATLAB Compiler SDK Version 7.1 (R2022b) MATLAB Report Generator Version 5.13 (R2022b) Mapping Toolbox Version 5.4 (R2022b) Mixed-Signal Blockset Version 2.3 (R2022b) Model Predictive Control Toolbox Version 8.0 (R2022b) Navigation Toolbox Version 2.3 (R2022b) Optimization Toolbox Version 9.4 (R2022b) Parallel Computing Toolbox Version 7.7 (R2022b) Partial Differential Equation Toolbox Version 3.9 (R2022b) Phased Array System Toolbox Version 4.8 (R2022b) Powertrain Blockset Version 1.12 (R2022b) Predictive Maintenance Toolbox Version 2.6 (R2022b) RF Blockset Version 8.4 (R2022b) RF Toolbox Version 4.4 (R2022b) Requirements Toolbox Version 2.1 (R2022b) Risk Management Toolbox Version 2.1 (R2022b) Robotics System Toolbox Version 4.1 (R2022b) Robust Control Toolbox Version 6.11.2 (R2022b) Sensor Fusion and Tracking Toolbox Version 2.4 (R2022b) SerDes Toolbox Version 2.4 (R2022b) Signal Processing Toolbox Version 9.1 (R2022b) SimBiology Version 6.4 (R2022b) SimEvents Version 5.13 (R2022b) Simscape Version 5.4 (R2022b) Simscape Driveline Version 3.6 (R2022b) Simscape Electrical Version 7.8 (R2022b) Simscape Fluids Version 3.5 (R2022b) Simscape Multibody Version 7.6 (R2022b) Simulink 3D Animation Version 9.5 (R2022b) Simulink Check Version 6.1 (R2022b) Simulink Code Inspector Version 4.2 (R2022b) Simulink Coder Version 9.8 (R2022b) Simulink Control Design Version 6.2 (R2022b) Simulink Coverage Version 5.5 (R2022b) Simulink Design Optimization Version 3.12 (R2022b) Simulink Design Verifier Version 4.8 (R2022b) Simulink PLC Coder Version 3.7 (R2022b) Simulink Real-Time Version 8.1 (R2022b) Simulink Report Generator Version 5.13 (R2022b) Simulink Test Version 3.7 (R2022b) Stateflow Version 10.7 (R2022b) Statistics and Machine Learning Toolbox Version 12.4 (R2022b) Symbolic Math Toolbox Version 9.2 (R2022b) System Identification Toolbox Version 10.0 (R2022b) Text Analytics Toolbox Version 1.9 (R2022b) Vehicle Dynamics Blockset Version 1.9 (R2022b) Vehicle Network Toolbox Version 5.3 (R2022b) Vision HDL Toolbox Version 2.6 (R2022b) WLAN Toolbox Version 3.5 (R2022b) Wavelet Toolbox Version 6.2 (R2022b) Wireless HDL Toolbox Version 2.5 (R2022b)
I am not aware of any way that a license number can show up as 0
Thanks for you response, but I'm not following what you mean. I never said I was expecting a license number to show up as 0, or in any way compare to 0.
I don't actually care about license numbers at all, I only care about figuring out whether a given user on a given machine has a MATLAB license on that machine.
@Mario Malic posted an example of ver with MATLAB License Number: 0 shown. As far as I known, license number 0 is not possible for any valid license.
Ahh, my bad, @Walter Roberson. The responses got collapsed when I read it. I only saw your response and assumed it was a direct response to my question, without seeing @Mario Malic's post for context.
@Mario Malic: To further clarify, what I'm trying to do - I'm trying to determine if a given user even has a valid MATLAB license to begin with before attempting to launch MATLAB. So using MATLAB's "ver" function doesn't help in this case, since the user running our script may not have a MATLAB license at all.
@Walter Roberson: If you run ver in the forum, the license number 0 is shown. See also:
license
ans = '0'

Sign in to comment.

Answers (3)

Fangjun Jiang
Fangjun Jiang on 19 Jan 2023
Edited: Fangjun Jiang on 19 Jan 2023
There are so many ways to specify the license file, I think you need to follow this order specified in "MATLAB License Search Path" in this link.
Instead, I would suggest launching MATLAB directly. If not successful, then use Python.

2 Comments

Thanks for your response. I assume you're talking about this section in the link you sent me?
--- begin snippet ---
MATLAB License Search Path
MATLAB reads the license file at startup to check for product licensing. MATLAB searches for the license file in the following locations, in the specified order:
  1. The license file specified on MATLAB startup command line using the -c option. The -c option overrides the entire search order and uses only what was specified on the command line. This is the only path MATLAB searches.
  2. Environment variables MLM_LICENSE_FILE and LM_LICENSE_FILE.
  3. The profile folder of the person starting the program
  4. The \licenses folder in the MATLAB installation for files named license.dat or any file with the extension .lic.
After it finds a MathWorks license file, MATLAB stops searching.
--- end snippet ---
If that's the case, then I think I'm Ok.
Cases 1 and 2 don't apply. My bash shell script is the one launching MATLAB (or numpy) and I'm not using the -c option, nor are we setting/using the environment variables.
Case 3 is the easy case I described in my original post as the user having their MATLAB license file in the following folder /home/<user>/.matlab/R2021a_licenses
That only leaves Case 4, which is the one I'm trying to solve, where license files from multiple users are all stored in /usr/local/MATLAB/R2021a/licenses and I somehow need to figure whether any of them belong to the current user
@Jan has pointed out the expired license or offline with license server.
One more complication, a license file could be license.dat or any *.lic file.

Sign in to comment.

Walter Roberson
Walter Roberson on 19 Jan 2023
Moved: Walter Roberson on 19 Jan 2023
If I look in /usr/local/MATLAB/R2021a/licenses I might see the following files:
license_MyHost_123456_R2021a.lic, license_MyHost_987654_R2021a.lic
In /usr/local/MATLAB/R2021a/licenses you should only see three kinds of licenses:
  • trial licenses can go there
  • Dedicated Host licenses can go there
  • Network licenses (that contain nothing user-specific) can go there
Individual user licenses will not go there.
A license file name format such as license_MyHost_123456_R2021a.lic in that directory would be used for a Dedicated Host license for host name MyHost with MAC address 123456 (if this were Windows then instead of MAC address there could be a disk serial number.) However at the moment I cannot rule out the possibility that they are trial licenses.
Those are not individual user licenses -- not unless your system has been configured with an LM_LICENSE environment variable that points to /usr/local/MATLAB/R2021a/licenses . If so, then in that particular case, the 123456 would be the Mathworks license number. Unfortunately if that is the case, then those files would not contain any information about username. And in such a situation, MATLAB would go through all of the .lic files in directory order, checking to see if someone else is already using that license, and grabbing the first one that is not in use. Which is not how you would want to configure a group of individual Standalone Named User licenses, but that is what would happen if someone did configure that way. (I believe.)

2 Comments

Maybe our license administrator set them up incorrectly, but I can guarantee that the files I'm finding in /usr/local/MATLAB/R2021a/licenses are indeed individual user licenses. Whether they work correctly for those users or not, I don't know since my license file is located under /home/<my_username>/.matlab/R2021a_licenses
For posting here, I did obfuscate the filename, but on the actual machine the files are named with the following format: license_<machine name>_<license number>_R2021a.lic
I have figured out the solution however! In the license files there are string segments that reads like the following:
HOSTID=MATLAB_HOSTID=010203040506:666f6f
where the "010203040506" would be the MAC address of this machine, and the "666f6f" is the username converted from the original ascii format of "foo" to the hexadecimal representation of those character values ('f' = 0x66, 'o' = 0x6f). So now all I have to do is build up a hexadecimal string of the user's username, then grep through each license file to see if any of them contain the user's username hexadecimal string

Sign in to comment.

Jan
Jan on 19 Jan 2023
The detection is not trivial. A matching license file might be there, but the license can be expired. The computer or license server might be offline.
The best way to check, if a user can run Matlab is to call Matlab and check the succeess. If it fails, your program can store a flag in its settings to avoid following checks.

4 Comments

Good point about the license being expired - I hadn't considered that.
I guess I was trying to avoid call MATLAB in cases where I can tell in advance that the user isn't licensed at all. I think my solution above at least handles that case. After that, I'll attempt to launch MATLAB as the user, and if it fails, fall back to numpy.
@Sean Murphy: Even if you find a license file (or several of them), the call to Matlab can fail: Missing toolbox, expired license, bad configuration (crashing startup function) or too old version. Therefore you need an error handling at all, which catchs the problem and calls the workaround.
This error handling solves the problem of the license check exhaustively already. So introducing this check is an unneeded source of bugs only.
I understand what you're saying, but I disagree. There are a few simple ways (as described above) that I can discover that a given user doesn't have a MATLAB license at all. In those cases, there's no point in ever even attempting to launch MATLAB. Launching MATLAB from the command line takes a couple of seconds and prints out an error message, which just clutters up the terminal.
So as a first step, I think checking for this is appropriate. Then as the next step, if I think they have a license, I can attempt to launch MATLAB and handle the case of MATLAB failing to launch, regardless of whether it's an invalid license problem, or some other issue.
@Sean Murphy: Yes, of course a check is not inaproppriate. Keep in mind that there can be more ways to run Matlab successfully than you are aware of. Only a successful run of Matlab is a reliable proof, that Matlab runs successfully, which include all kind of versions to perform the license check. Finding a license file is no trustworthy evidence, that the Matlab license is working.
There have been generic license files form Matlab 7, which can be provided from individual locations, which are defined by command line arguments.
Automatic tests are based on assumptions. Maybe letting the user decide actively is more reliable, e.g. with a "-useMatlab" flag in the command line or by a dialog window.

Sign in to comment.

Categories

Products

Release

R2021a

Tags

Asked:

on 19 Jan 2023

Commented:

Jan
on 22 Jan 2023

Community Treasure Hunt

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

Start Hunting!