What determines the current working directory of the Tomcat Java process?

My production server starts Linux using System V initialization scripts.

Tomcat is raised by running service tomcat6 start as the root user ( service starts the init script under cwd / ).

Tomcat then serves a web page to write the result of new File(".").getAbsolutePath() , which shows /usr/share/tomcat6/.

But Tomcat init script ( /etc/init.d/tomcat6 ) does not mention CWD, and it does not have cd .

Given that Java itself cannot change the current working directory, how did it happen that /usr/share/tomcat6 became the current working directory of Tomcat? Where does cdd change during startup?

Linux is about CentOS6.

+6
source share
2 answers

On CentOS 6, the Tomcat init.d script starts tomcat with this line:

 $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} start-security" 

$ SU is either / bin / runuser or / bin / su, $ TOMCAT_USER is usually "tomcat", and $ TOMCAT_SCRIPT is usually "/ usr / sbin / tomcat6". "su -" or "runuser -" executes its command as the specified user from the specified user's home directory. Thus, this command will change to the user ID "tomcat" and the home directory, then run / usr / sbin / tomcat 6. Tomcat6 script will eventually run tomcat itself.

The tomcat user home directory should be the same as CATALINA_BASE. In short, the "su" or "runuser" command is what sets the current working directory to CATALINA_BASE.

init.d script is not an official part of tomcat; it is provided by a package supporter, and it may differ from system to system. On my Ubuntu 13 system, /etc/init.d/tomcat6 contains the cd for $ CATALINA_BASE.

Tomcat startup scripts (bin / startup.sh, etc.) do not set the working directory. When I launch tomcat 6 or tomcat 7 directly using my own script run, it just inherits the working directory from which I ran it.

Remember that on Linux you can see the current current directory of the process by checking /proc/<pid>/cwd .

+5
source

Have you seen the variables?

  • CATALINA_HOME : This is the root of your Tomcat installation. When we say: β€œThis information can be found in your CATALINA_HOME/README.txt file,” we want to look at the README.txt in the root of your Tomcat installation.

  • CATALINA_BASE Optionally, Tomcat can be configured for multiple instances by defining $ CATALINA_BASE for each instance. If multiple instances are not configured, CATALINA_BASE same as CATALINA_HOME .

In the apache-tomcat-7.0.42/bin/catalina.sh file, you will see:

 # Only set CATALINA_HOME if not already set [ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." >/dev/null; pwd` # Copy CATALINA_BASE from CATALINA_HOME if not already set [ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME" 
0
source

Source: https://habr.com/ru/post/953664/


All Articles