This tutorial will discuss two ways to start or stop the Apache Tomcat service on your Linux machine.
Method 1 – Startup Scripts
Apache Tomcat comes with startup scripts that you can use to start or stop the service. This method applies when you have an Apache Tomcat server installed as a binary release using a .zip or .tar archive.
If so, start by navigating to the bin directory of the Apache Tomcat as:
Inside the bin directory, you can view all the scripts to manage the service using the ls command:
To start the Apache Tomcat service using its startup script, run the script as:
NOTE: Ensure you have executed permissions for the scripts in this directory.
Once you execute the startup script, you should see an output indicating whether the Tomcat service is up.
Using CATALINA_HOME: /home/debian/apache-tomcat-10.0.10
Using CATALINA_TMPDIR: /home/debian/apache-tomcat-10.0.10/temp
Using JRE_HOME: /usr
Using CLASSPATH: /home/debian/apache-tomcat-
10.0.10/bin/bootstrap.jar:/home/debian/apache-tomcat-10.0.10/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
By default, the Apache Tomcat service runs on port 8080; you can verify if it’s running using the lsof utility as:
The above command should show port 8080 as LISTEN:
java 1562 debian 41u IPv6 1598 0t0 TCP *:8080 (LISTEN)
java 1562 debian 52u IPv6 19670 0t0 TCP 127.0.0.1:8005 (LISTEN)
To stop the Apache Tomcat service, run the shutdown script as:
You should get an example output as shown below:
Using CATALINA_HOME: /home/debian/apache-tomcat-10.0.10
Using CATALINA_TMPDIR: /home/debian/apache-tomcat-10.0.10/temp
Using JRE_HOME: /usr
Using CLASSPATH: /home/debian/apache-tomcat-
10.0.10/bin/bootstrap.jar:/home/debian/apache-tomcat-10.0.10/bin/tomcat-juli.jar
Using CATALINA_OPTS:
---OUTPUT-TRUNCATED-----------------------
Adding to PATH
Although starting up Apache Tomcat using its startup scripts is easy, you must specify the full path or be in the bin directory.
To resolve this, you can add the directory to the $PATH variable using the command:
Once you have the apache tomcat directory in your path, you can run the scripts from any location without specifying the full path.
You can also use the catalina.sh script to start and stop the apache service.
For example:
catalina.sh stop
To start and stop the apache service respectively.
Method 2 – Using Systemctl
Another way to manage the Apache Tomcat service is to use systemctl. This method will work if Apache Tomcat is installed from the system repositories such as APT and RPM.
To start the tomcat service, use the command:
To stop the service, enter the command:
Using Custom Unit File
You will notice that the above commands only work if you have the Apache Tomcat server installed from the system packages.
If you have Tomcat installed manually from a zip or tar package, you can create a custom unit file to manage the service using the systemd.
The following is an example tomcat.service file.
sudo vim /etc/systemd/system/tomcat.service
Enter the unit file contents as:
Description="Apache Tomcat"
After=network.target
Service]
Type=forking
User=debian
Group=debian
Environment="JAVA_HOME=/usr/lib/jvm/java-11-amazon-corretto/"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
Environment="CATALINA_BASE=/home/debian/apache-tomcat-10.0.10"
Environment="CATALINA_HOME=/home/debian/apache-tomcat-10.0.10"
Environment="CATALINA_PID=/home/debian/apache-tomcat-10.0.10/temp/tomcat.pid"
Environment="CATALINA_OPTS="
ExecStart=/home/debian/apache-tomcat-10.0.10/bin/startup.sh
ExecStop=/home/debian/apache-tomcat-10.0.10/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
Save the file and close.
Next, reload the system daemon to load the new unit file as:
Finally, manage the tomcat service using the system as:
sudo systemctl stop tomcat.service
Using the methods discussed above, you can now manage the Apache Tomcat service easily.
Conclusion
This article has covered two ways to start and stop the Apache Tomcat service depending on various installation methods.