繁体   English   中英

init.d脚本可在控制台中完美运行,而在systemd的服务调用中效果不佳

[英]init.d scripts works perfectly in console, bad poorly in systemd's service call

我有以下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在服务团队开始时只写一行:

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

非常感谢

这是我缝合在一起的一个简单的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的安装。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM