CLOVER🍀

That was when it all began.

Apache Tomcatをフォアグラウンドで起動する

これは、なにをしたくて書いたもの?

Apache Tomcatをフォアグラウンドで起動する方法について、ちょっとメモしておこうかなと。
Dockerコンテナやsystemdのサービスとして起動する時に、知っておくと便利そうですよね。

前に書いたエントリーで触れたことがあったのですが、その時は別のテーマにフォーカスしたので、独立して、と。

結論を書くと、catalina.sh runで実行すればOKです。

環境

今回の環境は、こちら。

$ java --version
openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 17.0.2+8-Ubuntu-120.04, mixed mode, sharing)

使用するApache Tomcatは、こちらです。

$ curl -LO https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.18/bin/apache-tomcat-10.0.18.tar.gz
$ tar xf apache-tomcat-10.0.18.tar.gz
$ cd apache-tomcat-10.0.18

startup.shとshutdown.sh

Apache Tomcatでよく使うスクリプトといえば、startup.shshutdown.shです。

この中を見ると、実体はcatalina.shであることがわかります。

startup.sh

$ tail -n 19 bin/startup.sh
EXECUTABLE=catalina.sh

# Check that target executable exists
if $os400; then
  # -x will Only work on the os400 if the files are:
  # 1. owned by the user
  # 2. owned by the PRIMARY group of the user
  # this will not work if the user belongs in secondary groups
  eval
else
  if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
    echo "Cannot find $PRGDIR/$EXECUTABLE"
    echo "The file is absent or does not have execute permission"
    echo "This file is needed to run this program"
    exit 1
  fi
fi

exec "$PRGDIR"/"$EXECUTABLE" start "$@"

shutdown.sh

$ tail -n 19 bin/shutdown.sh
EXECUTABLE=catalina.sh

# Check that target executable exists
if $os400; then
  # -x will Only work on the os400 if the files are:
  # 1. owned by the user
  # 2. owned by the PRIMARY group of the user
  # this will not work if the user belongs in secondary groups
  eval
else
  if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
    echo "Cannot find $PRGDIR/$EXECUTABLE"
    echo "The file is absent or does not have execute permission"
    echo "This file is needed to run this program"
    exit 1
  fi
fi

exec "$PRGDIR"/"$EXECUTABLE" stop "$@"

それぞれ、catalina.shstartstopが呼び出されています。

catalina.sh

ここでcatalina.shに書かれているコメントを見てみます。

# For supported commands call "catalina.sh help" or see the usage section at
# the end of this file.

helpでヘルプが見れそうなので、表示。

$ bin/catalina.sh help
Using CATALINA_BASE:   /path/to/apache-tomcat-10.0.18
Using CATALINA_HOME:   /path/to/apache-tomcat-10.0.18
Using CATALINA_TMPDIR: /path/to/apache-tomcat-10.0.18/temp
Using JRE_HOME:        /usr/lib/jvm/default
Using CLASSPATH:       /path/to/apache-tomcat-10.0.18/bin/bootstrap.jar:/path/to/apache-tomcat-10.0.18/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Usage: catalina.sh ( commands ... )
commands:
  debug             Start Catalina in a debugger
  debug -security   Debug Catalina with a security manager
  jpda start        Start Catalina under JPDA debugger
  run               Start Catalina in the current window
  run -security     Start in the current window with security manager
  start             Start Catalina in a separate window
  start -security   Start in a separate window with security manager
  stop              Stop Catalina, waiting up to 5 seconds for the process to end
  stop n            Stop Catalina, waiting up to n seconds for the process to end
  stop -force       Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
  stop n -force     Stop Catalina, wait up to n seconds and then use kill -KILL if still running
  configtest        Run a basic syntax check on server.xml - check exit code for result
  version           What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined

startは"separate window"で、バックグラウンドにいくわけですね。runは"current window"なのでこちらはフォアグラウンドのようです。

というわけで、実行。

$ bin/catalina.sh run

Apache Tomcatがフォアグラウンドで起動します。

15-Mar-2022 22:47:30.098 情報 [main] org.apache.coyote.AbstractProtocol.start プロトコルハンドラー ["http-nio-8080"] を開始しました。
15-Mar-2022 22:47:30.147 情報 [main] org.apache.catalina.startup.Catalina.start サーバーの起動 [1015]ms

停止する場合は、Ctrl-cということになります。

ログは、そのままだとcatalina.out以外は出力されます。

$ ll logs
合計 20
drwxr-x--- 2 xxxxx xxxxx 4096  3月 15 22:47 ./
drwxrwxr-x 9 xxxxx xxxxx 4096  3月 15 22:42 ../
-rw-r----- 1 xxxxx xxxxx 7909  3月 15 22:47 catalina.2022-03-15.log
-rw-r----- 1 xxxxx xxxxx  414  3月 15 22:47 localhost.2022-03-15.log
-rw-r----- 1 xxxxx xxxxx    0  3月 15 22:47 localhost_access_log.2022-03-15.txt

このあたりの動作を変更したかったら、Apache Tomcatのログ設定を変更、というところでしょうね。

ちょっとしたメモでした。