SoFunction
Updated on 2025-03-08

Use systemctl to manage Tomcat startup, stop, restart and boot

Preface

This article mainly introduces to you the relevant content on systemctl management of Tomcat startup, stop, restart and boot. It is shared for your reference and learning. I won’t say much below, let’s take a look at the detailed introduction together.

1. Create a service

When using service to manage services, a script file is created in the /etc// directory to manage the start and stop of the service. In systemctl, it is similar. The file directory is different. Create a script file tomcat in the /lib/systemd/system directory, and the contents are as follows:

[Unit]
Description=Tomcat
After=

[Service]
Type=forking
PIDFile=/usr/local/tomcat/pid
ExecStart=/usr/local/tomcat/bin/ start
ExecReload=/usr/local/tomcat/bin/ restart
ExecStop=/usr/local/tomcat/bin/ stop

[Install]
WantedBy=

[Unit] means this is the basic information

  • Description is a description
  • After is started after that service, usually after the network service is started.

[Service] means that this is service information

  • Type is the service type
  • PIDFile is the service's pid file path. After opening, the CATALINA_PID parameter must be added to the bin/ of tomcat.
  • ExecStart is the command to start the service
  • ExecReload is a command to restart the service
  • ExecStop is a command to stop service

[Install] means that this is installation-related information

  • In which way WantedBy is started: It indicates that when the system starts in a multi-user mode (the default run level), the service needs to be run automatically.

When adding the CATALINA_PID parameter to the bin/ of tomcat, you need to add it on #OS specific support.

CATALINA_PID=/usr/local/tomcat/pid

# OS specific support. $var _must_ be set to either true or false.

cygwin=false
....slightly..

2. Create a soft link

The soft link is created to automatically start the service when the system is initialized next.

ln -s /lib/systemd/system/ /etc/systemd/system//

Creating soft links is like a shortcut in Windows

ln -s is to create soft links

ln -s Original file Target file (decision address of shortcut)

If an exception occurs when creating a soft connection, don't worry. Check whether the /etc/systemd/system// directory creates a soft link normally. Sometimes an error is just a prompt, but it actually succeeds.

$ ll /etc/systemd/system//
total 8
drwxr-xr-x 2 root root 4096 Mar 30 15:46 ./
drwxr-xr-x 13 root root 4096 Mar 13 14:18 ../
lrwxrwxrwx 1 root root 31 Nov 23 14:43  -> /lib/systemd/system/
...slightly...

3. Refresh the configuration

The service you just configured needs to be recognized by systemctl, so you must refresh the configuration.

$ systemctl daemon-reload

If you do not have permission, you can use sudo

$ sudo systemctl daemon-reload

4. Start, restart, stop

Start tomcat

$ systemctl start tomcat

Restart tomcat

$ systemctl restart tomcat

Stop tomcat

$ systemctl stop tomcat

5. Start up automatically

Tomcat service added to boot

$ systemctl enable tomcat

Prohibit booting

$ systemctl disable tomcat

6. Check the status

View status

$ systemctl status tomcat

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.