- How to Load Environment Variables in a Cron Job | Baeldung on Linux
- Environment variable - MATLAB getenv (mathworks.com)
- Execute operating system command and return output - MATLAB system (mathworks.com)
- linux - Where can I set environment variables that crontab will use? - Stack Overflow
- bash - How to simulate the environment cron executes a script with? - Stack Overflow
why does 'getenv' return empty when Matlab is launched from crontab?
16 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
Karan Singh
2023-9-29
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
0 个评论
Walter Roberson
2023-9-29
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Third-Party Cluster Configuration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!