Tomcat Java进程的当前工作目录由什么决定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18753663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What determines the current working directory of Tomcat Java process?
提问by
My production server runs Linux using System V style init scripts.
我的生产服务器使用 System V 风格的初始化脚本运行 Linux。
Tomcat is brought up by running service tomcat6 start
as root user (service
runs init script under cwd /
).
通过service tomcat6 start
以 root 用户身份运行(service
在 cwd 下运行 init 脚本/
)来启动 Tomcat 。
Tomcat then serves a web page to write the result of new File(".").getAbsolutePath()
, which shows /usr/share/tomcat6/.
然后 Tomcat 提供一个网页来写入 的结果new File(".").getAbsolutePath()
,它显示/usr/share/tomcat6/.
But Tomcat init script (/etc/init.d/tomcat6
) makes no mention of CWD, neither does it have any cd
command.
但是Tomcat init script( /etc/init.d/tomcat6
)没有提到CWD,也没有任何cd
命令。
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?
鉴于Java本身不能改变当前工作目录,那么/usr/share/tomcat6
Tomcat的当前工作目录是怎么来的呢?在它的启动过程中哪里改变了它的 cwd?
The Linux in question is CentOS6.
有问题的 Linux 是 CentOS6。
采纳答案by Kenster
On CentOS 6, the Tomcat init.d script launches tomcat by means of this line:
在 CentOS 6 上,Tomcat init.d 脚本通过以下行启动 tomcat:
$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.
$SU 是/bin/runuser 或/bin/su,$TOMCAT_USER 通常是“tomcat”,而$TOMCAT_SCRIPT 通常是“/usr/sbin/tomcat6”。“su -”或“runuser -”以指定用户的身份从指定用户的主目录运行其命令。所以这个命令将更改为“tomcat”用户ID和主目录,然后运行/usr/sbin/tomcat6。tomcat6 脚本最终会启动 tomcat 本身。
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.
tomcat 用户的主目录应该与 CATALINA_BASE 相同。简而言之,这里的“su”或“runuser”命令将当前工作目录设置为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.
init.d 脚本不是 tomcat 的正式组成部分;它是由包维护者提供的,它可以从一个系统到另一个系统不同。在我的 Ubuntu 13 系统上,/etc/init.d/tomcat6
包含一个cd
到 $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.
Tomcat 自己的启动脚本(bin/startup.sh 等)不设置工作目录。当我直接使用自己的启动脚本启动 tomcat 6 或 tomcat 7 时,它只是继承了我运行它的工作目录。
Remember that on Linux, you can see any process's actual current directory by checking /proc/<pid>/cwd
.
请记住,在 Linux 上,您可以通过检查/proc/<pid>/cwd
.
回答by Paul Vargas
Did you see the variables?
你看到变量了吗?
CATALINA_HOME
: This represents the root of your Tomcat installation. When we say, "This information can be found in yourCATALINA_HOME/README.txt
file" we mean to look at theREADME.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 asCATALINA_HOME
.
CATALINA_HOME
:这代表您的 Tomcat 安装的根目录。当我们说“此信息可以在您的CATALINA_HOME/README.txt
文件中找到”时,我们的意思是查看README.txt
Tomcat 安装根目录下的文件。CATALINA_BASE
或者,可以通过为每个实例定义 $CATALINA_BASE 来为多个实例配置 Tomcat。如果没有配置多个实例,CATALINA_BASE
则与CATALINA_HOME
.
In the file apache-tomcat-7.0.42/bin/catalina.sh
you will see:
在文件中,apache-tomcat-7.0.42/bin/catalina.sh
您将看到:
# 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"