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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:45:05  来源:igfitidea点击:

What determines the current working directory of Tomcat Java process?

javalinuxtomcat

提问by

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

我的生产服务器使用 System V 风格的初始化脚本运行 Linux。

Tomcat is brought up by running service tomcat6 startas root user (serviceruns 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 cdcommand.

但是Tomcat init script( /etc/init.d/tomcat6)没有提到CWD,也没有任何cd命令。

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

鉴于Java本身不能改变当前工作目录,那么/usr/share/tomcat6Tomcat的当前工作目录是怎么来的呢?在它的启动过程中哪里改变了它的 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/tomcat6contains a command to cdto $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 your CATALINA_HOME/README.txtfile" we mean to look at the README.txtfile at the root of your Tomcat install.

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

  • CATALINA_HOME:这代表您的 Tomcat 安装的根目录。当我们说“此信息可以在您的CATALINA_HOME/README.txt文件中找到”时,我们的意思是查看README.txtTomcat 安装根目录下的文件。

  • CATALINA_BASE或者,可以通过为每个实例定义 $CATALINA_BASE 来为多个实例配置 Tomcat。如果没有配置多个实例,CATALINA_BASE则与CATALINA_HOME.

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