天天看点

Linux 为tomcat 添加服务到systemctl准备工作添加服务问题参考

Linux 为 tomcat 添加服务到systemctl

  • 准备工作
    • 查看tomcat安装位置,即文件夹位置
    • 查看JAVA_HOME
    • 添加setenv.sh文件
  • 添加服务
  • 问题
    • stop service faild
    • `==== AUTHENTICATING FOR org.freedesktop.systemd1.manager-unit ====`
  • 参考

准备工作

查看tomcat安装位置,即文件夹位置

sudo find / -name *tomcat*

从输出结果中找到tomcat所在位置。假设是

/opt/tomcat/

查看JAVA_HOME

echo $JAVA_HOME

这是查看当前系统配置的环境变量JAVA_HOME。假设是

/usr/lib/jvm/openJDK8/

添加setenv.sh文件

到tomcat安装目录下的bin目录中添加setenv.sh文件,即

/opt/tomcat/bin/setenv.sh

文件中添加内容:

CATALINA_PID="$CATALINA_BASE/tomcat.pid"

tomcat.pid

的名字可以自定义,这个无所谓,比如

curtomcat.pid

,应该只要是扩展名为pid就行。

添加服务

/lib/systemd/system/

中添加文件

tomcat8.service

,同样,文件名可以自定义,这个是到时候启动服务的时候的服务名。

/lib/systemd/system/tomcat8.service

的内容为:

[Unit]
Description=Apache Tomcat 8
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
Environment="JAVA_HOME=/usr/lib/jvm/openJDK8/"
PIDFile=/opt/tomcat/tomcat.pid
ExecStart=/opt/tomcat/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
# usually, tomcat service will be killed with exit_code 143.
SuccessExitStatus=143
PrivateTmp=true
#User=root
#Group=root

[Install]
WantedBy=multi-user.target
           

其中的JAVA_HOME应该是可以随便指定自已想用的jdk;

tomcat.pid

是刚刚设置那个pid文件名。

执行

systemctl daemon-reload

,来刷新服务。

接下来就可以用

systemctl start tomcat8

来启动服务了。

问题

stop service faild

● tomcat8.service - Apache Tomcat 8
   Loaded: loaded (/usr/lib/systemd/system/tomcat8.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2021-05-21 13:46:07 +08; 9s ago
  Process: 8937 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
  Process: 8804 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 8815 (code=exited, status=143)
           

因为退出的code 是143 而不是0,所以状态是failed。在tomcat8.service中添加的

SuccessExitStatus=143

就是让结束时,认为143时正常的。

==== AUTHENTICATING FOR org.freedesktop.systemd1.manager-unit ====

每次使用systemctl启动或者停止服务时,都要授权,麻烦。

修改

/usr/share/polkit-1/actions/org.freedesktop.systemd1.policy

.(我不确定其中1是小写的L还是数字1。用tab补全吧)

把其中id="org.freedesktop.systemd1.manage-units"改为如下

<action id="org.freedesktop.systemd1.manage-units">
                <description gettext-domain="systemd">Manage system services or other units</description>
                <message gettext-domain="systemd">Authentication is required to manage system services or other units.</message>
                <defaults>
                        <allow_any>yes</allow_any>
                        <allow_inactive>auth_admin</allow_inactive>
                        <allow_active>auth_admin_keep</allow_active>
                </defaults>
        </action>
           

allow_any 的值为yes。

参考

1.linux下tomcat加入到系统服务systemctl

2. Linux tomcat加入Systemctl服务,开机自启动

3. Services remain in failed state after stopped with systemctl

4. Authentication is required to manage system services or units.

5. polkit

继续阅读