How do I start Tomcat as a service on Linux?

Asked

Viewed 22,495 times

5

I need to put a WEB system into production using the Tomcat 7. I know I can start with the shell catalina.sh start but I would like something to work after the reboot and Tomcat to come in normally.

3 answers

5

Assuming Tomcat7 is installed in /usr/local/tomcat you can put the following shell in the directory /etc/init. d

You must create a Tomcat user so that you can run the shell inside his sandbox.

Filing cabinet: 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 username tomcat is not mandatory. It may be tomcat7 for example. The important thing is that the name is significant in managing your software assets.

Installing

To add 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 on runlevel 3.

More details than one Runlevel see this link

To start executing the script without a Rebbot do:

service tomcat7 start 

To check if it is running, you can check through the ps -ef | grep catalina

Completion

chkconfig creates symbolic links pointing to the shell script in /etc/init. d within the directories of the corresponding runlevel : /etc/rc[0-6]. d

Check the directory:

ls -lA /etc/rc3.d 

Note that the symbolic links either start with S (start) or K (Kill) followed by a number representing your upload sequence.

The loading sequence is specified in the following line of the script:

chkconfig: 345 98 99 

Start/finish numbers may be the same.

Uninstalling

If you need to restore the system as it was before all this do:

service tomcat7 stop
update-rc.d -f tomcat7 remove
rm -f /etc/init.d/tomcat7

This will remove the chkconfig treatment you entered.

3

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 permissions to the script (as administrator):

chmod -x /etc/init.d/tomcat7
update-rc.d tomcat7 defaults

And finally, you can start the service:

service tomcat7 start
  • You could also use sudo -u $OWNER "..." to be able to perform the operation with some user defined as in the other reply.

0

Follow the step by step for installing Tomcat 9 and Service on Linux, updated script is on 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.