How do I start Tomcat as a service on Linux?


I need to put a web system in production using Tomcat 7 . I know I can start with the shell start but I would like something that works after reboot and that Tomcat comes in normally.

asked by anonymous 19.03.2014 / 00:45

3 answers


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

# chkconfig: 345 98 99
# description: Tomcat auto start-stop script.
# Set OWNER to the user id of the owner of the Tomcat software.


case "$1" in
        su - $OWNER -c "/usr/local/tomcat/bin/ start >> /usr/local/tomcat/logs/tomcat7.log 2>&1"
        touch /var/lock/subsys/tomcat7
        su - $OWNER -c "/usr/local/tomcat/bin/ stop  >> /usr/local/tomcat/logs/tomcat7.log 2>&1"
        rm -f /var/lock/subsys/tomcat7
        echo "Uso: $0 {start|stop}" 

# 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.


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


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

Check 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.


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.

19.03.2014 / 01:18

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 start

# Outro código ... etc...

exit 0;

Another way is to create a script in /etc/init.d/tomcat7 . Containing:


# 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


start() {
 sh /usr/share/tomcat7/bin/

stop() {
 sh /usr/share/tomcat7/bin/

case $1 in
  start|stop) $1;;
  restart) stop; start;;
  *) echo "Run as $0 <start|stop|restart>"; exit 1;;

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
19.03.2014 / 01:08

Follow the step by step installation of Tomcat 9 and Service on Linux, updated script is in this link .

### Install Tomcat 9 + JRE8 on Ubuntu, Debian, CentOS, OpenSUSE 64Bits
### URL com Screencast de Instalação do Tomcat9
### Link:

## 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

groupadd tomcat && useradd -M -s /bin/nologin -g tomcat -d /usr/local/tomcat tomcat

cd /usr/local/
wget --header 'Cookie: oraclelicense=a'
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
export PATH' >> /etc/profile

source /etc/profile
java -version

cd /usr/local/
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
Description=Apache Tomcat Web Application Container
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
ExecStop=/bin/kill -15 $MAINPID
[Install]' > /etc/systemd/system/tomcat.service

systemctl daemon-reload
systemctl start tomcat
systemctl enable tomcat

## Open in web browser:
## http://server_IP_address:8080
09.06.2017 / 09:52