无法启动 tomcat:未找到单元 tomcat.service

无法启动 tomcat:未找到单元 tomcat.service

我尝试在 ubuntu 18.04 上启动 tomcat 8,出现以下错误:

root@Openkm:/home/becsa# service tomcat start
Failed to start tomcat.service: Unit tomcat.service not found.

root@Openkm:/home/becsa# ls -lisa /etc/init.d/
total 200
8912955  4 drwxr-xr-x   2 root root  4096 Jul  2 16:50 .
8912897 12 drwxr-xr-x 126 root root 12288 Jul  2 16:51 ..
8914482  4 -rwxr-xr-x   1 root root  2269 Apr 22  2017 acpid
8914483  8 -rwxr-xr-x   1 root root  5336 Jan 23  2017 alsa-utils
8914484  4 -rwxr-xr-x   1 root root  2014 May 29  2017 anacron
8914485  8 -rwxr-xr-x   1 root root  4335 Mar 22  2018 apparmor
8917373  4 -rwxr-xr-x   1 root root  2805 Feb 27 05:18 apport
8914487  4 -rwxr-xr-x   1 root root  2401 Aug 22  2018 avahi-daemon
8914488  4 -rwxr-xr-x   1 root root  2968 Feb  5  2018 bluetooth
8914489  4 -rwxr-xr-x   1 root root  1232 Apr 19  2018 console-setup.sh
8914490  4 -rwxr-xr-x   1 root root  3049 Nov 16  2017 cron
8914493  4 -rwxr-xr-x   1 root root  2804 Mar 27  2018 cups
8914494  4 -rwxr-xr-x   1 root root  1961 Feb 26  2018 cups-browsed
8914495  4 -rwxr-xr-x   1 root root  2813 Nov 16  2017 dbus
8914496  4 -rwxr-xr-x   1 root root  1172 Jun  6  2016 dns-clean
8914497  4 -rwxr-xr-x   1 root root  3033 Oct  9  2018 gdm3
8914498  4 -rwxr-xr-x   1 root root   985 Feb  4  2019 grub-common
8914499  4 -rwxr-xr-x   1 root root  3809 Feb 15  2018 hwclock.sh
8914500  4 -rwxr-xr-x   1 root root  2444 Oct 25  2017 irqbalance
8914501  4 -rwxr-xr-x   1 root root  3131 May 19  2017 kerneloops
8914502  4 -rwxr-xr-x   1 root root  1479 Feb 16  2018 keyboard-setup.sh
8914503  4 -rwxr-xr-x   1 root root  2044 Aug 15  2017 kmod
8917142  8 -rwxr-xr-x   1 root root  5930 Jan 30 09:25 mysql
8914508  8 -rwxr-xr-x   1 root root  4597 Nov 25  2016 networking
8914507  4 -rwxr-xr-x   1 root root  1942 Mar 26  2018 network-manager
8913080  4 -rwxr-xr-x   1 root root  1282 Jul  2 16:25 openkm
8914509  4 -rwxr-xr-x   1 root root  1366 Jan 17  2018 plymouth
8914510  4 -rwxr-xr-x   1 root root   752 Jan 17  2018 plymouth-log
8914511  4 -rwxr-xr-x   1 root root   612 Feb 26  2018 pppd-dns
8914512  4 -rwxr-xr-x   1 root root  1191 Jan 18  2018 procps
8914513  8 -rwxr-xr-x   1 root root  4355 Dec 13  2017 rsync
8914514  4 -rwxr-xr-x   1 root root  2864 Jan 14  2018 rsyslog
8914515  4 -rwxr-xr-x   1 root root  2333 Aug 10  2017 saned
8914516  4 -rwxr-xr-x   1 root root  2117 Dec 15  2017 speech-dispatcher
8914517  4 -rwxr-xr-x   1 root root  2484 Jan 20  2017 spice-vdagent
8914506  4 -rwxr-xr-x   1 root root  3837 Jan 26  2018 ssh
8914518  8 -rwxr-xr-x   1 root root  5974 Apr 20  2018 udev
8914519  4 -rwxr-xr-x   1 root root  2083 Aug 15  2017 ufw
8914520  4 -rwxr-xr-x   1 root root  1391 Dec 13  2018 unattended-upgrades
8914521  4 -rwxr-xr-x   1 root root  1306 Oct 15  2018 uuidd
8914522  4 -rwxr-xr-x   1 root root   485 Apr  2  2015 whoopsie
8914523  4 -rwxr-xr-x   1 root root  2757 Jan 20  2017 x11-common

答案1

从官方网站下载并解压 tomcat 时,Tomcat 服务不会默认安装。您需要手动创建它:

  1. tomcat.service在文件夹中创建新文件/etc/systemd/system/

    $ sudo nano /etc/systemd/system/tomcat.service
    
  2. 添加以下几行:

    [Unit]
    Description=Tomcat9
    After=network.target
    
    [Service]
    Type=forking
    
    Environment=CATALINA_PID=/opt/tomcat/tomcat9.pid
    Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
    Environment=CATALINA_HOME=/opt/tomcat
    Environment=CATALINA_BASE=/opt/tomcat
    Environment="CATALINA_OPTS=-Xms512m -Xmx512m"    # optional
    Environment="JAVA_OPTS=-Dfile.encoding=UTF-8"    # optional
    ExecStart=/opt/tomcat/bin/startup.sh
    ExecStop=/opt/tomcat/bin/shutdown.sh
    
    [Install]
    WantedBy=multi-user.target
    
  3. 保存文件并重新启动服务管理器:

    $ sudo systemctl daemon-reload
    
  4. 启动Tomcat服务并检查其状态:

    $ sudo service tomcat start
    $ sudo service tomcat status
    

相关内容