V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
warcraft1236
V2EX  ›  Linux

关于.service 文件的编写

  •  
  •   warcraft1236 · 2017-11-13 12:26:14 +08:00 · 5023 次点击
    这是一个创建于 2353 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在 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 配置文件中配置这些变量呢?

    第 1 条附言  ·  2017-11-13 18:05:22 +08:00

    感谢大家的回复,现在找到了能正常运行的方法

    [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
    
    19 条回复    2017-11-14 17:38:29 +08:00
    ryd994
        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
    swulling
        2
    swulling  
       2017-11-13 12:37:18 +08:00 via iPhone
    apt install tomcat8
    warcraft1236
        3
    warcraft1236  
    OP
       2017-11-13 12:48:07 +08:00
    @ryd994 我试过 type=forking,还是不行
    chust
        4
    chust  
       2017-11-13 13:17:16 +08:00
    hcymk2
        5
    hcymk2  
       2017-11-13 13:21:24 +08:00
    7654
        6
    7654  
       2017-11-13 13:27:34 +08:00
    Environment
    journalctl 查看下启动日志
    riggzh
        7
    riggzh  
       2017-11-13 13:27:49 +08:00
    ubuntu 自己不是可以直接 apt install 一个带 services 的 tomcat 么(只要不是服务器上,我都这么干,省事)
    doubleflower
        9
    doubleflower  
       2017-11-13 13:57:31 +08:00
    把 Wants=network.target 这一行去了试试
    pq
        10
    pq  
       2017-11-13 14:03:36 +08:00
    把 systemd 对 rc.local 的支持打开,然后通过它来启动吧,省事多了。。。
    lain0
        11
    lain0  
       2017-11-13 14:04:00 +08:00
    systemd 只是啟動服務不需要 enable。

    systemctl enable <service> 的意思是把 <service> 設置成在 systemd 啟動時自動啟動。
    warcraft1236
        12
    warcraft1236  
    OP
       2017-11-13 14:08:55 +08:00
    @doubleflower 为啥要去掉这个呢?
    FullBridgeRect
        13
    FullBridgeRect  
       2017-11-13 14:33:23 +08:00 via Android   ❤️ 1
    @pq 讲真这样用不清真,迟早爆炸
    zjp
        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 这个是肯定的
    Tyanboot
        15
    Tyanboot  
       2017-11-13 17:45:27 +08:00 via Android
    … 你不把日志发出来怎么排错。至少得把 systemctl status tomcat 的日志放出来
    iwishing
        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 "$@"
    ryd994
        17
    ryd994  
       2017-11-14 00:47:27 +08:00
    呃,其实你可以用 EnvironmentFile 这个,不然每次都有 systemctl daemon-reload
    warcraft1236
        18
    warcraft1236  
    OP
       2017-11-14 09:49:00 +08:00
    @ryd994 啥意思,能不能详细讲一下
    openbsd
        19
    openbsd  
       2017-11-14 17:38:29 +08:00
    现在不用 jsvc 了吗 ?
    官方手册又没提供最新的做法 ?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3018 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:31 · PVG 08:31 · LAX 17:31 · JFK 20:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.