user972946
user972946

Reputation:

What determines the current working directory of Tomcat Java process?

My production server runs Linux using System V style init scripts.

Tomcat is brought up by running service tomcat6 start as root user (service runs 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) makes no mention of CWD, neither does it have any cd command.

Given that Java itself cannot change current working directory, then how come /usr/share/tomcat6 became Tomcat's current working directory? Where in its startup process changes its cwd?

The Linux in question is CentOS6.

Upvotes: 9

Views: 20457

Answers (2)

Kenster
Kenster

Reputation: 25390

On CentOS 6, the Tomcat init.d script launches tomcat by means of this line:

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

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

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

The init.d script isn't formally part of tomcat; it's supplied by the package maintainer, and it can be different from one system to another. On my Ubuntu 13 system, /etc/init.d/tomcat6 contains a command to cd to $CATALINA_BASE.

Tomcat's own startup scripts (bin/startup.sh and so on) don't set a working directory. When I launch tomcat 6 or tomcat 7 directly using its own startup script, it just inherits the working directory that I ran it from.

Remember that on Linux, you can see any process's actual current directory by checking /proc/<pid>/cwd.

Upvotes: 10

Paul Vargas
Paul Vargas

Reputation: 42020

Did you see the variables?

  • CATALINA_HOME: This represents the root of your Tomcat installation. When we say, "This information can be found in your CATALINA_HOME/README.txt file" we mean to look at the README.txt file at the root of your Tomcat install.

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

In the file apache-tomcat-7.0.42/bin/catalina.sh 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"

Upvotes: 0

Related Questions