I need to put a web system in production using Tomcat 7 . I know I can start with the shell catalina.sh start
but I would like something that works after reboot and that Tomcat comes in normally.
I need to put a web system in production using Tomcat 7 . I know I can start with the shell catalina.sh start
but I would like something that works after reboot and that Tomcat comes in normally.
Assuming Tomcat7 installed on /usr/local/tomcat
you can put the following shell in the /etc/init.d directory
You must create a tomcat user so that you can run the shell inside the sandbox.
File: tomcat7
#!/bin/sh
# chkconfig: 345 98 99
# description: Tomcat auto start-stop script.
#
# Set OWNER to the user id of the owner of the Tomcat software.
OWNER=tomcat
case "$1" in
'start')
su - $OWNER -c "/usr/local/tomcat/bin/catalina.sh start >> /usr/local/tomcat/logs/tomcat7.log 2>&1"
touch /var/lock/subsys/tomcat7
;;
'stop')
su - $OWNER -c "/usr/local/tomcat/bin/catalina.sh stop >> /usr/local/tomcat/logs/tomcat7.log 2>&1"
rm -f /var/lock/subsys/tomcat7
;;
*)
echo "Uso: $0 {start|stop}"
RETVAL=1
esac
# RESULT='ps -ef | grep catalina | grep -v grep '
# echo $RESULT
exit $RETVAL
The user name tomcat
is not required. It can be tomcat7
for example. The important thing is that the name is significant in the management of your software assets.
Installing
To add the shell script to chkconfig do:
cd /etc/init.d
chmod 755 tomcat7
update-rc.d tomcat7 defaults
With the last command, we are telling the system to start the script automatically in runlevel 3.
More details of what a RunLevel see this link
To start running the script without a Rebbot do:
service tomcat7 start
To check if you are running, you can check with ps -ef | grep catalina
Conclusion
chkconfig creates symbolic links pointing to the shell script in /etc/init.d within the corresponding runlevel directories: / etc / rc [0-6] .dCheck the directory:
ls -lA /etc/rc3.d
Note that symbolic links either start with S (start) or K (kill) followed by a number that represents your load sequence.
The load sequence is specified in the following line of the script:
chkconfig: 345 98 99
The initialization / finalization numbers can be the same.
Uninstalling
If you need to restore the system as it was before then do everything:
service tomcat7 stop
update-rc.d -f tomcat7 remove
rm -f /etc/init.d/tomcat7
This will remove the chkconfig treatment you entered.
You can run this script in /etc/rc.local
.
This file runs right after boot.
The format should look something like this:
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will “exit 0″ on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing
catalina.sh start
# Outro código ... etc...
exit 0;
Another way is to create a script in /etc/init.d/tomcat7
.
Containing:
#!/bin/bash
### BEGIN INIT INFO
# Provides: tomcat7
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop Tomcat server
### END INIT INFO
PATH=/sbin:/bin:/usr/sbin:/usr/bin
start() {
sh /usr/share/tomcat7/bin/startup.sh
}
stop() {
sh /usr/share/tomcat7/bin/shutdown.sh
}
case $1 in
start|stop) $1;;
restart) stop; start;;
*) echo "Run as $0 <start|stop|restart>"; exit 1;;
esac
Remembering to give the script permissions (as an administrator):
chmod -x /etc/init.d/tomcat7
update-rc.d tomcat7 defaults
And finally, you can start the service:
service tomcat7 start
Follow the step by step installation of Tomcat 9 and Service on Linux, updated script is in this link .
#!/bin/bash
### Install Tomcat 9 + JRE8 on Ubuntu, Debian, CentOS, OpenSUSE 64Bits
### URL com Screencast de Instalação do Tomcat9
### http://www.linuxpro.com.br/2017/04/instalando-tomcat-9-no-ubuntu/
### Link: https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-centos-7
## First install wget
## Primeiro instale o wget
# Check if user has root privileges
if [[ $EUID -ne 0 ]]; then
echo "You must run the script as root or using sudo"
exit 1
fi
groupadd tomcat && useradd -M -s /bin/nologin -g tomcat -d /usr/local/tomcat tomcat
cd /usr/local/
wget --header 'Cookie: oraclelicense=a' http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jre-8u131-linux-x64.tar.gz
tar -xf jre-8u131-linux-x64.tar.gz && rm -f jre-8u131-linux-x64.tar.gz
mv jre1.8.0_131 java
echo 'JAVA_HOME=/usr/local/java
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH' >> /etc/profile
source /etc/profile
java -version
cd /usr/local/
wget http://mirror.nbtelecom.com.br/apache/tomcat/tomcat-9/v9.0.0.M19/bin/apache-tomcat-9.0.0.M19.tar.gz
tar -xvf apache-tomcat-9.0.0.M19.tar.gz
mv apache-tomcat-9.0.0.M19 tomcat
rm -f apache-tomcat-9.0.0.M19.tar.gz
cd /usr/local/tomcat
chgrp -R tomcat conf
chmod g+rwx conf
chmod g+r conf/*
chown -R tomcat webapps/ work/ temp/ logs/
chown -R tomcat:tomcat *
chown -R tomcat:tomcat /usr/local/tomcat
echo '# Systemd unit file for tomcat
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/local/java
Environment=CATALINA_PID=/usr/local/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat
Environment=CATALINA_BASE=/usr/local/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target' > /etc/systemd/system/tomcat.service
systemctl daemon-reload
systemctl start tomcat
systemctl enable tomcat
## Open in web browser:
## http://server_IP_address:8080