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

时间:2016-03-10 15:03:59

标签: linux init systemd

我有以下init.d脚本。

#!/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

如果您直接从控制台使用它

/etc/init.d/teamcity start

,效果很好。但是系统的“服务”命令不起作用。 Systemd无法正常启动Teamcity。

Systemd在服务teamcity start上只写了一行:

Started LSB: Start daemon at boot time.

停止服务失败:

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服务表示法是我的最后一个解决方案。

非常感谢

1 个答案:

答案 0 :(得分:0)

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

[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变量以适合您的Java和TeamCity安装。