Shell rc.d daemon script not picking up global environment variables

Hello,

I have created a rc.d daemon script that runs our Java application, which I will call java_app in this discussion. Our Java applications required a global environment variable to be set in order for the application to run correctly. If that environment variable is not set, the application will fail to start.

I have set this global environment variable in /etc/csh.cshrc and /etc/profile. However, when I start the service via "service java_app start", the service fails because the global environment variable is not set.

I corrected this by setting the environment variable in the java_app script, but this is not the ideal solution. I would like to leverage the global environment variable and not set it in the java_app script.

Is there something I am missing?
 
Code:
#       ${name}_env     n       Environment variables to run ${command} with.
#
#       ${name}_env_file n      File to source variables to run ${command} with.
try one of those in rc.conf ${name} should be java_app
assuming you have a standard startup script based on rc.subr
 
Yes, I did add ${name}_env as you mentioned; however, as I mentioned in my post, that is not the most ideal solution... simply because I have to change the script in order to change the variable instead of simply changing the environment variable...

Here is my scenario...

I have a global environment variable name LOG_DIRECTORY, it is set to /usr/local/log, for example. I have this set in /etc/csh.cshrc, and /etc/profile. When the Java applications starts, it checks to make sure this global environment variable is set correctly and then uses it. If it is not set correctly, the applications stops...

If I run and application without being a service, it runs just fine. When I run it using the script, it fails... saying the the LOG_DIRECTORY environment variable is not set correctly. So I added ${name}_env="LOG_DIRECTORY=/usr/local/log", to the service script. That indeed worked and the applications starts correctly...

However, that is not the ideal solution. Why is the script NOT picking up the global environment variable from either /etc/csh.cshrc or /etc/profile?
 
simply because I have to change the script in order to change the variable instead of simply changing the environment variable...
You're doing it wrong. You put this in rc.conf, not change the rc(8) script. You should create a proper rc(8) script for your 'java_app'.
Why is the script NOT picking up the global environment variable from either /etc/csh.cshrc or /etc/profile?
First of all, /etc/csh.cshrc is for the C shells. No script should ever be written in C shell. Scripts (specifically rc(8) scripts) are written using sh(1). Second issue, there is a difference in how the shell is started. Interactive, login or not, all determine which start scripts are run. Only a login shell will read /etc/profile. Scripts aren't login shells. The third issue is, there's no such thing as a "global" environment variable.
 
Thanks for the feedback, it is much appreciated.

I have been looking for a sample of a proper rc script to for running Java applications... Unfortunately, I have not found a good one... I will keep reading...
 
The closest guide I can think of for an example framework rc script is something like xcfb daemon for jenkins.

The environment variables you need can be loaded by the load_rc_config function (sourced form /etc/rc.subr), which "dots in" in the configuration file(s) for a given service -- so you can configure the thing using rc.conf, just like other daemons.

[Plus you get a guide on how to get a virtual frame buffer if your Java programmers live in the last century.]
 
Back
Top