首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >init.d脚本在控制台中运行良好,在systemd的服务调用中运行不佳

init.d脚本在控制台中运行良好,在systemd的服务调用中运行不佳
EN

Stack Overflow用户
提问于 2016-03-10 23:03:59
回答 1查看 548关注 0票数 0

我有以下init.d脚本。

代码语言:javascript
复制
#!/bin/sh
### BEGIN INIT INFO
# Provides:          teamcity
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

start_cmd="start-stop-daemon --start  -c root --chdir /srv/teamcity/TeamCity-9.1/bin --exec  /srv/teamcity/TeamCity-9.1/bin/runAll.sh start"
stop_cmd="start-stop-daemon --start  -c root --chdir /srv/teamcity/TeamCity-9.1/bin --exec  /srv/teamcity/TeamCity-9.1/bin/runAll.sh stop"
user="root"

export TEAMCITY_DATA_PATH="/srv/teamcity/TeamCity-9.1/.BuildServer"
export TEAMCITY_PID_FILE_PATH="/var/run/teamcity.pid"
export TEAMCITY_SERVER_OPTS=-Djava.awt.headless=true
export TEAMCITY_SERVER_MEM_OPTS="-Xmx750m -XX:MaxPermSize=270m"
/etc/profile.d/java.sh

name="teamcity"
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"

get_pid() {
    cat "$pid_file"
}

is_running() {
    [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}

case "$1" in
    start)
    if is_running; then
        echo "Already started"
    else
        echo "Starting $name"
        $start_cmd >> "$stdout_log" 2>> "$stderr_log" &
        sleep 1
        if ! is_running; then
            echo "Unable to start, see $stdout_log and $stderr_log"
            exit 1
        fi
    fi
    ;;
    stop)
    if is_running; then
        echo -n "Stopping $name.."
        $stop_cmd >> "$stdout_log" 2>> "$stderr_log" &
        for i in {1..20}
        do
            if ! is_running; then
                break
            fi

            echo -n "."
            sleep 1
        done

        if is_running; then
            echo "Not stopped; may still be shutting down or shutdown may have failed"
            exit 1
        else
            echo "Stopped"
            if [ -f "$pid_file" ]; then
                rm "$pid_file"
            fi
        fi
    else
        echo "Not running"
    fi
    ;;
    restart)
    $0 stop
    if is_running; then
        echo "Unable to stop, will not attempt to start"
        exit 1
    fi
    $0 start
    ;;
    status)
    if is_running; then
        echo "Running"
    else
        echo "Stopped"
        exit 1
    fi
    ;;
    *)
    echo "Usage: $0 {start|stop|restart|status}"
        exit 1
    ;;
esac

exit 0

如果您直接从控制台使用它,如下所示

代码语言:javascript
复制
/etc/init.d/teamcity start

,它工作得很好。但与系统的“服务”命令不起作用。Systemd无法正确启动Teamcity。

Systemd在service teamcity start上只写了一行:

代码语言:javascript
复制
Started LSB: Start daemon at boot time.

停止服务失败,出现以下错误:

代码语言:javascript
复制
teamcity.service: control process exited, code=exited status=1
Stopped LSB: Start daemon at boot time.
Unit teamcity.service entered failed state.

我花了三天时间搜索和修改init文件,但没有合适的解决方案。有什么建议可以解决这个问题吗?

离开SYSV init.d脚本,开始使用新的systemd服务表示法是我最后的解决方案。

非常感谢

EN

回答 1

Stack Overflow用户

发布于 2017-02-20 14:03:45

下面是一个简单的systemd服务文件,它是我拼接在一起的,适用于我安装的TeamCity版本:v10.0.4 (build 42538)

代码语言:javascript
复制
[Unit]
Description=TeamCity Build Agent
After=network.target

[Service]
Type=simple
Environment="JAVA_HOME=/usr/java/jdk1.8.0_121"
ExecStart=/opt/teamcity/bin/startup.sh
ExecStop=/opt/teamcity/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

您需要定制EnvironmentExecStartExecStop变量以适合您的TeamCity和Java安装。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35920134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档