Set environment variable in shell script/access in Tomcat Application
Asked Answered
W

4

10

I want to add and environment variable which can access by my tomcat web-app. I have gone through this link but i want to set environment variable in root user. How to do that?

Welsh answered 14/4, 2012 at 11:19 Comment(0)
W
0

got the solution...what i have done is i have put the export statements in /etc/init.d/tomcat6 at top and restarted the server by command sudo /etc/init.d/tomcat6 restart. So now my web-app running in tomcat server can access that variable.

Welsh answered 25/4, 2012 at 8:59 Comment(3)
I don't believe it's a good idea to modify the native Apache script, /etc/init.d/tomcat6. Your changes may interfere with a future update, plus you're modifying default Tomcat behavior. Seems hacky to me.Begay
I agree with you. Please suggest me any other way to do it. I have set all the environment on AWS CloudFormation for automation in AWS Infrastructure. So probably it will do all the things automatically. But still looking for the better suggestion.Welsh
I'm not sure if you ever got it to work using the AWS userdata, cause I'm pretty sure tomcat doesn't pick those up. Please see my answer above for an alternative.Keele
K
9

According to the docs (http://tomcat.apache.org/tomcat-7.0-doc/RUNNING.txt) you should set all env vars in $CATALINA_HOME/bin/setenv.sh

EDIT: For completeness, I guess it's worth mentioning that even though this is the recommended way, the docs above state that:

By default the setenv script file is absent. If the script file is present both in CATALINA_BASE and in CATALINA_HOME, the one in CATALINA_BASE is preferred.

In case it is absent, you might also want to look for env vars in:

  • /etc/tomcat/tomcat[67].conf (suse) or
  • /etc/default/tomcat[67].conf (e.g. ubuntu) or
  • /etc/sysconfig/tomcat[67].conf (rhel, fedora)
Keele answered 11/12, 2014 at 7:52 Comment(0)
A
1

This is how you can do it

  1. sudo su and cd to /var/lib/tomcat8/bin/ (or whichever is your tomcat bin path)
  2. touch setenv.sh(if it doesn't exist), if file present already do 'vi setenv.sh'
  3. chmod 777 setenv.sh (make file executable)
  4. vi setenv.sh and set following line in setenv.sh export key=value
  5. sudo systemctl restart tomcat.service

In your java file you can use the following code to check if the variable is set

private static void printEnv() {
    System.out.println("******************************Environment Vars*****************************");
    Map<String, String> enviorntmentVars = System.getenv();
    enviorntmentVars.entrySet().forEach(System.out::println);

    System.out.println("******************************system Vars*****************************");
    Properties enviorntmentProperties = System.getProperties();
    enviorntmentVars.entrySet().forEach(System.out::println);
}
Afar answered 17/10, 2020 at 8:14 Comment(1)
This doesn't seem to work. The environment variables don't get set in Tomcat.Alarmist
W
0

got the solution...what i have done is i have put the export statements in /etc/init.d/tomcat6 at top and restarted the server by command sudo /etc/init.d/tomcat6 restart. So now my web-app running in tomcat server can access that variable.

Welsh answered 25/4, 2012 at 8:59 Comment(3)
I don't believe it's a good idea to modify the native Apache script, /etc/init.d/tomcat6. Your changes may interfere with a future update, plus you're modifying default Tomcat behavior. Seems hacky to me.Begay
I agree with you. Please suggest me any other way to do it. I have set all the environment on AWS CloudFormation for automation in AWS Infrastructure. So probably it will do all the things automatically. But still looking for the better suggestion.Welsh
I'm not sure if you ever got it to work using the AWS userdata, cause I'm pretty sure tomcat doesn't pick those up. Please see my answer above for an alternative.Keele
S
-1

Doesnt this work?

Go to your environment file. sudo vi /etc/environment and Add the required variable. and save the file.

I think in the recent Ubuntu, You would have to restart your system for the changes to take effect.

Slavocracy answered 14/4, 2012 at 12:9 Comment(4)
But i cant restart the system.Is there any way to do this without restarting the system?Welsh
perhaps touch /etc/environment and source /etc/environment may do the trick.Slavocracy
Did not work for me, had to modify /etc/init.d/tomcat7 and add export FOO=barGeorgena
According to Ubuntu documentation when running sudo service service_name start, the environment from /etc/environment is not referenced. See also question: stackoverflow.com/questions/16645430Georgena

© 2022 - 2024 — McMap. All rights reserved.