why does 'getenv' return empty when Matlab is launched from crontab?

15 views (last 30 days)
I have a bash script that invokes a matlab function:
#!/bin/bash
echo $HOSTNAME
/path/to/MATLAB/install/matlab -softwareopengl -nosplash -r "testscript;exit;"
testcript.m is just:
host = getenv('HOSTNAME')
When I run the bash script from the command line, the linux command line outputs "machine_name" followed by matlab displaying the same machine name.
When I run the script from a cronjob, matlab shows the output of getenv as empty.
Why is it that, even though the bash script knows the environment variable 'HOSTNAME', matlab sees the environment variable as empty?
If I add the following to my bash script, matlab executes as desired:
export HOSTNAME=$HOSTNAME
Why does matlab need me to explicitly assign $HOSTNAME to the environment variable when run from a cron?

Answers (2)

Karan Singh
Karan Singh on 29 Sep 2023
Hi Daniel,
From what I understand, there is a bash script that invokes a MATLAB function and when running the script from the command line, MATLAB correctly sees the value of HOSTNAME and displays it. But when running the script from a cronjob, MATLAB sees the HOSTNAME environment variable as empty and does not display any value.
When running a script from a cronjob, the environment variables available to the script may differ from those available in an interactive shell session. This is because cron jobs run in a separate environment with a minimal set of environment variables.
In your case, the HOSTNAME environment variable is not automatically available to the MATLAB process when running from a cronjob. When you run the script directly from the command line, the HOSTNAME environment variable is inherited from the shell and MATLAB can access it using getenv('HOSTNAME'). However, when running from a cronjob, the HOSTNAME environment variable is not automatically passed to the MATLAB process.
By explicitly exporting the HOSTNAME environment variable in your bash script with export HOSTNAME=$HOSTNAME, you are ensuring that the variable is available to the MATLAB process. This line exports the value of the HOSTNAME variable from the shell environment to the environment of the subsequent MATLAB process.
Thus, by adding this line to your bash script, you are explicitly making the HOSTNAME environment variable available to MATLAB, even when running from a cronjob.
Attached below are some documentation links that you may find helpful:
Hope this helps!
Karan Singh Khati

Walter Roberson
Walter Roberson on 29 Sep 2023
In shells derived from the Bourne shell, when you create an environment variable, by default the environment variable is not made available to processes created from the shell script. The environment variable needs to be "exported" to be available to subprocesses.
The work-around is the one you already found, to use export the way you did. Or, equivalently, use set -x

Categories

Find more on Third-Party Cluster Configuration in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!