技术解析

关于.service 文件的编写
0
2021-06-28 02:54:29
idczone

我在 ubuntu 上下了个 tomcat,想随机启动,查了一下是用 systemd 来起

可是我写了如下内容的 tomcat.service 文件

[Unit]
Description=Tomcat Service
After=network.target
Wants=network.target

[Service]
Type=simple
PIDFile=/var/run/tomcat.pid
ExecStart=/root/apache-tomcat-9.0.1/bin/catalina.sh start
Restart=on-failure

[Install]
WantedBy=multi-user.target

使用 systemctl start tomcat.service 提示失败(有 enable )

网上搜了一波,没看到有关于这些的详解

我感觉应该是 java_home 没有读取到?那么我应该怎么在 service 配置文件中配置这些变量呢?


http://lmgtfy.com/?q=systemd+service+file+environment+variables
另外,先看看 status 会有很多信息
看了一下 catalina.sh ,看起来是一个服务管理脚本,执行完立刻退出,这是不能直接用 simple 管理的。因为 systemd 假设前台进程就是主进程,主进程退出就杀掉所有其他。用 systemd 管理这样的服务有两种办法:
1. 直接启动真正的服务进程,长期服务的主进程
2. Type=forking

apt install tomcat8

我试过 type=forking,还是不行

http://lmgtfy.com/?q=Tomcat+9+systemd

没有错误日志?
你可以自己 debug 下
https://serverfault.com/questions/803844/how-to-debug-systemd-unit-execstart

Environment
journalctl 查看下启动日志

ubuntu 自己不是可以直接 apt install 一个带 services 的 tomcat 么(只要不是服务器上,我都这么干,省事)

https://www.freedesktop.org/software/systemd/man/systemd.service.html

把 Wants=network.target 这一行去了试试

把 systemd 对 rc.local 的支持打开,然后通过它来启动吧,省事多了。。。

systemd 只是啟動服務不需要 enable。
systemctl enable 的意思是把 設置成在 systemd 啟動時自動啟動。

为啥要去掉这个呢?

讲真这样用不清真,迟早爆炸

折腾过 Systemd 启动 jetty,tomcat 不是很熟悉。传递 java_home 这些参数,得看 tomcat 会读取哪些配置文件,一般有 /etc/default/xxx,或者在.service 文件用 Environment 指定
Type=forking 这个是肯定的

… 你不把日志发出来怎么排错。至少得把 systemctl status tomcat 的日志放出来

ExecStart=/root/apache-tomcat-9.0.1/bin/startup.sh
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
/>http://www.apache.org/licenses/LICENSE-2.0
/>Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-----------------------------------------------------------------------------
Start Script for the CATALINA Server
-----------------------------------------------------------------------------
Better OS/400 detection: see Bugzilla 31132
os400=false
case "`uname`" in
OS400*) os400=true;;
esac
resolve links - $0 may be a softlink
PRG="$0"
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`/"$link"
fi
done
PRGDIR=`dirname "$PRG"`
EXECUTABLE=catalina.sh
Check that target executable exists
if $os400; then
-x will Only work on the os400 if the files are:
1. owned by the user
2. owned by the PRIMARY group of the user
this will not work if the user belongs in secondary groups
eval
else
if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
echo "Cannot find $PRGDIR/$EXECUTABLE"
echo "The file is absent or does not have execute permission"
echo "This file is needed to run this program"
exit 1
fi
fi
exec "$PRGDIR"/"$EXECUTABLE" start "[email protected]"

呃,其实你可以用 EnvironmentFile 这个,不然每次都有 systemctl daemon-reload

啥意思,能不能详细讲一下

现在不用 jsvc 了吗 ?
官方手册又没提供最新的做法 ?

数据地带为您的网站提供全球顶级IDC资源
在线咨询
专属客服