简体   繁体   中英

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

i have following init.d script.

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

If you using it directly from console like this

/etc/init.d/teamcity start

,it works very well. But with system'd "service" command not works. Systemd is not be able to start Teamcity properly.

Systemd writes only one line on service teamcity start:

Started LSB: Start daemon at boot time.

Stopping service fails with:

teamcity.service: control process exited, code=exited status=1
Stopped LSB: Start daemon at boot time.
Unit teamcity.service entered failed state.

I spend three days googling and changing init file, but no suitable solution. Any suggestions to fix that?

Leave SYSV init.d scripts and start using new systemd services notation is my last solution.

Thanks a lot

Here's a simple systemd service file that I stitched together that works for my installed version of 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

You'll need to customize the Environment , ExecStart , and ExecStop variables to suit your installation of Java and TeamCity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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