我在 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 配置文件中配置这些变量呢?
感谢大家的回复,现在找到了能正常运行的方法
[Unit]
Description=Tomcat9
After=network.target
[Service]
Type=forking
Environment=CATALINA_PID=/var/run/tomcat9.pid
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
Environment=CATALINA_HOME=/root/apache-tomcat-9.0.1
Environment=CATALINA_BASE=/root/apache-tomcat-9.0.1
Environment="CATALINA_OPTS=-Xms512m -Xmx512m"
Environment="JAVA_OPTS=-Dfile.encoding=UTF-8 -Dnet.sf.ehcache.skipUpdateCheck=true -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+UseParNewGC"
ExecStart=/root/apache-tomcat-9.0.1/bin/catalina.sh start
[Install]
WantedBy=multi-user.target
1
ryd994 2017-11-13 12:32:53 +08:00 via Android
http://lmgtfy.com/?q=systemd+service+file+environment+variables
另外,先看看 status 会有很多信息 看了一下 catalina.sh ,看起来是一个服务管理脚本,执行完立刻退出,这是不能直接用 simple 管理的。因为 systemd 假设前台进程就是主进程,主进程退出就杀掉所有其他。用 systemd 管理这样的服务有两种办法: 1. 直接启动真正的服务进程,长期服务的主进程 2. Type=forking |
2
swulling 2017-11-13 12:37:18 +08:00 via iPhone
apt install tomcat8
|
3
warcraft1236 OP @ryd994 我试过 type=forking,还是不行
|
4
chust 2017-11-13 13:17:16 +08:00
|
5
hcymk2 2017-11-13 13:21:24 +08:00
|
6
7654 2017-11-13 13:27:34 +08:00
Environment
journalctl 查看下启动日志 |
7
riggzh 2017-11-13 13:27:49 +08:00
ubuntu 自己不是可以直接 apt install 一个带 services 的 tomcat 么(只要不是服务器上,我都这么干,省事)
|
8
pisser 2017-11-13 13:31:26 +08:00
|
9
doubleflower 2017-11-13 13:57:31 +08:00
把 Wants=network.target 这一行去了试试
|
10
pq 2017-11-13 14:03:36 +08:00
把 systemd 对 rc.local 的支持打开,然后通过它来启动吧,省事多了。。。
|
11
lain0 2017-11-13 14:04:00 +08:00
systemd 只是啟動服務不需要 enable。
systemctl enable <service> 的意思是把 <service> 設置成在 systemd 啟動時自動啟動。 |
12
warcraft1236 OP @doubleflower 为啥要去掉这个呢?
|
13
FullBridgeRect 2017-11-13 14:33:23 +08:00 via Android 1
@pq 讲真这样用不清真,迟早爆炸
|
14
zjp 2017-11-13 14:36:34 +08:00 via Android
折腾过 Systemd 启动 jetty,tomcat 不是很熟悉。传递 java_home 这些参数,得看 tomcat 会读取哪些配置文件,一般有 /etc/default/xxx,或者在.service 文件用 Environment 指定
Type=forking 这个是肯定的 |
15
Tyanboot 2017-11-13 17:45:27 +08:00 via Android
… 你不把日志发出来怎么排错。至少得把 systemctl status tomcat 的日志放出来
|
16
iwishing 2017-11-13 17:57:05 +08:00
ExecStart=/root/apache-tomcat-9.0.1/bin/startup.sh
#!/bin/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 "$@" |
17
ryd994 2017-11-14 00:47:27 +08:00
呃,其实你可以用 EnvironmentFile 这个,不然每次都有 systemctl daemon-reload
|
18
warcraft1236 OP @ryd994 啥意思,能不能详细讲一下
|
19
openbsd 2017-11-14 17:38:29 +08:00
现在不用 jsvc 了吗 ?
官方手册又没提供最新的做法 ? |