简体   繁体   中英

Tomcat Start on Reboot: init.d script unsuccessful

I need to have tomcat start after a reboot of the linux OS. I cannot get init.d to function properly through reboot.

OS and Versions:

JRE: 1.8.0
JAVA: 1.8.0
Tomcat:  8.5.34
Linux:  Amazon Linux 2

****ALL STEPS COMPLETED AS ROOT

TOMCAT Deployment Configuration:

1)  Install tomcat 8.5.34 using a tar.gz gzip file
2)  configure /{$TOMCAT}/conf/server.xml to use 443 connectors
3)  Deploy MicroStrategy application through deploying a .war file on restart
4)  configure SSL keys using Java Key Store
5)  configure microstrategy webapp for SAML authentication using PING

init.d Script Deployment Configuration

Note: I have tried various scripts through /etc/init.d/tomcat and the chkconfig utility.
1) Create tomcat using vi 2) Insert script (I have tried numerous scripts, but this one seems to be the clostest to exactly what I need and the most explicit) 3) chmod 755 /etc/init.d/tomcat 4) chkconfig --add tomcat 5) chkconfig --level 2345 tomcat on (This command is not successful) 6) chkconfig --list tomcat (returns tomcat 0:off 1:off 2:off 3:on 4:on 5:on 6:off)

Testing of this script is successful:

./etc/init.d/tomcat start
./etc/init.d/tomcat stop
./etc/init.d/tomcat restart 

Confirmed that chkconfig created the links:

/etc/rc1.d  K20tomcat
/etc/rc2.d  K20tomcat
/etc/rc3.d  S82tomcat
/etc/rc4.d  S82tomcat
/etc/rc5.d  S82tomcat
/etc/rc6.d  K20tomcat

Script File for Tomcat

#!/bin/sh
#
# chkconfig: 345 82 20
#
# description:  Tomcat Service


JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
JRE_HOME=/usr/lib/jvm/jre-1.8.0-openjdk
CATALINA_HOME=/opt/apache-tomcat-8.5.34
export JAVA_HOME JRE_HOME CATALINA_HOME
case $1 in
start)
cd $CATALINA_HOME/bin
./startup.sh
;;
stop)
cd $CATALINA_HOME/bin
./shutdown.sh
;;
restart)
cd $CATALINA_HOME/bin
./shutdown.sh    
./startup.sh
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0

Expectations

I expect the base URL at {$TOMCAT}/webapps/ROOT to be accessible from the Public URL pointing to this device following a reboot. The tomcat services remain in a stopped state after reboot.

Any suggestions?

Amazon Linux 2 uses systemd service manager which should be backwards compatible with systemv init scripts provided that systemd-sysv-generator is executed to generate service units out of /etc/init.d scripts (not recommended in your case I think). Since you are writing the script yourself it is recommended that you write a proper service unit .
It's probable that such *.service file is already present on the tar.gz used to install tomcat.

Enable tomcat using systemd rather than systemv

Description

The script used is relatively simple because its only function is to start the server at reboot. I have established all of the required environment variables using setenv.sh in the TOMCAT bin.

Variables

  1. TEST: Any alphanumeric value
  2. TOMCAT_INSTALL_PATH: the location where you installed TOMCAT

Steps

  1. Create a file tomcat@.service in /etc/systemd/system
    • A template can be found in /etc/systemd/system/multi-user.target.wants/tomcat.service

tomcat@.service

[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking
Environment="CATALINA_HOME={TOMCAT_INSTALL_PATH}"
ExecStart=/opt/apache-tomcat-8.5.34/bin/startup.sh
ExecStop=$CATALINA_HOME/bin/shutdown.sh
SuccessExitStatus=143
User=root

[Install]
WantedBy=multi-user.target
  1. create empty tomcat.pid file (mine was in {TOMCAT_INSTALL_PATH}/conf
  2. write CATALINA_PID="{TOMCAT_INSTALL_PATH}/conf/tomcat.pid line to setenv.sh in the {TOMCAT_INSTALL_PATH}/bin行写入 {TOMCAT_INSTALL_PATH}/bin 中的 setenv.sh
  3. systemctl daemon-reload
  4. systemctl enable tomcat@test.service
  5. systemctl start tomcat@test.service

Troubleshoot

systemctl status tomcat@test.service -l

-This command will provide log output to the console which displays the output which is also written to the systemctl log. I received errors on initial setup because tomcat could not interpret which was the main tomcat process and would close when reading the end of other processes. This was overcome by creating a pid file in the /conf folder and setting the CATALINA_PID variable in setenv.sh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM