What determines the current working directory of Tomcat Java process?
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
.
Admin
Updated on July 26, 2022Comments
-
Admin almost 2 years
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 anycd
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.
-
Admin over 10 yearsI've certainly seen those env variables, but have not seen
cd $CATALINA_HOME
. -
Admin over 10 yearsAwesome. I just noticed that
/etc/passwd
defines tomcat user home directory as/usr/share/tomcat6
.