Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set environment variable in shell script/access in Tomcat Application

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?

like image 779
Anand Soni Avatar asked Apr 14 '12 11:04

Anand Soni


People also ask

How do I set an environment variable in shell?

You can set your own variables at the command line per session, or make them permanent by placing them into the ~/. bashrc file, ~/. profile , or whichever startup file you use for your default shell. On the command line, enter your environment variable and its value as you did earlier when changing the PATH variable.

How do I pass an environment variable in bash script?

Environment Variables Bash scripts can also be passed with the arguments in the form of environment variables. This can be done in either of the following ways: Specifying the variable value before the script execution command. Exporting the variable and then executing the script.

How do I set environment variables in bash?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.


2 Answers

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)
like image 129
gsaslis Avatar answered Nov 15 '22 16:11

gsaslis


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);
}
like image 27
Shubham Goel Avatar answered Nov 15 '22 17:11

Shubham Goel