一台服务器上有多个 Jenkins 服务器

一台服务器上有多个 Jenkins 服务器

我已经在一台服务器上安装了jenkins
我想将jenkins管理和开发任务分开。因此,我为管理和开发创建了 2 个配置jenkins
配置很好,运行良好。开发 jenkins 使用默认脚本
启动。 对于管理,我创建了原始脚本的副本并对其进行了修改。 问题是,这个修改后的脚本启动了管理员 但停止/重新启动了开发init.d
jenkinsinit.d jenkins
init.djenkinsjenkins.

#!/bin/sh
###############################################################################
#
# chkconfig: 35 99 01
# description: Jenkins Automation Server
###############################################################################
### BEGIN INIT INFO
# Provides:          jenkins_admin
# Required-Start:    $local_fs $remote_fs $network $time $named
# Should-Start: $time sendmail
# Required-Stop:     $local_fs $remote_fs $network $time $named
# Should-Stop: $time sendmail
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Jenkins Automation Server
# Description:       Jenkins Automation Server
### END INIT INFO
# Check for missing binaries (stale symlinks should not happen)
JENKINS_WAR="/usr/lib/jenkins/jenkins.war"
test -r "$JENKINS_WAR" || { echo "$JENKINS_WAR not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 5; fi; }
# Check for existence of needed config file and read it

# Here i changen path to config

JENKINS_CONFIG=/etc/sysconfig/jenkins_admin
test -e "$JENKINS_CONFIG" || { echo "$JENKINS_CONFIG not existing";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }
test -r "$JENKINS_CONFIG" || { echo "$JENKINS_CONFIG not readable. Perhaps you forgot 'sudo'?";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }

# Here i changed pid and lock files

JENKINS_PID_FILE="/var/run/jenkins_admin.pid"
JENKINS_LOCKFILE="/var/lock/subsys/jenkins_admin"
# Source function library.
. /etc/init.d/functions
# Read config
[ -f "$JENKINS_CONFIG" ] && . "$JENKINS_CONFIG"
# Set up environment accordingly to the configuration settings
[ -n "$JENKINS_HOME" ] || { echo "JENKINS_HOME not configured in $JENKINS_CONFIG";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }
[ -d "$JENKINS_HOME" ] || { echo "JENKINS_HOME directory does not exist: $JENKINS_HOME";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 1; fi; }
# Search usable Java as /usr/bin/java might not point to minimal version required by Jenkins.
# see http://www.nabble.com/guinea-pigs-wanted-----Hudson-RPM-for-RedHat-Linux-td25673707.html
candidates="
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-1.7.0/bin/java
/usr/lib/jvm/jre-1.7.0/bin/java
/usr/bin/java
"
for candidate in $candidates
do
  [ -x "$JENKINS_JAVA_CMD" ] && break
  JENKINS_JAVA_CMD="$candidate"
done
JAVA_CMD="$JENKINS_JAVA_CMD $JENKINS_JAVA_OPTIONS -DJENKINS_HOME=$JENKINS_HOME -jar $JENKINS_WAR"

# Here i changed cache and log directories

PARAMS="--logfile=/var/log/jenkins_admin/jenkins.log --webroot=/var/cache/jenkins_admin/war --daemon"
[ -n "$JENKINS_PORT" ] && PARAMS="$PARAMS --httpPort=$JENKINS_PORT"
[ -n "$JENKINS_LISTEN_ADDRESS" ] && PARAMS="$PARAMS --httpListenAddress=$JENKINS_LISTEN_ADDRESS"
[ -n "$JENKINS_HTTPS_PORT" ] && PARAMS="$PARAMS --httpsPort=$JENKINS_HTTPS_PORT"
[ -n "$JENKINS_HTTPS_KEYSTORE" ] && PARAMS="$PARAMS --httpsKeyStore=$JENKINS_HTTPS_KEYSTORE"
[ -n "$JENKINS_HTTPS_KEYSTORE_PASSWORD" ] && PARAMS="$PARAMS --httpsKeyStorePassword='$JENKINS_HTTPS_KEYSTORE_PASSWORD'"
[ -n "$JENKINS_HTTPS_LISTEN_ADDRESS" ] && PARAMS="$PARAMS --httpsListenAddress=$JENKINS_HTTPS_LISTEN_ADDRESS"
[ -n "$JENKINS_DEBUG_LEVEL" ] && PARAMS="$PARAMS --debug=$JENKINS_DEBUG_LEVEL"
[ -n "$JENKINS_HANDLER_STARTUP" ] && PARAMS="$PARAMS --handlerCountStartup=$JENKINS_HANDLER_STARTUP"
[ -n "$JENKINS_HANDLER_MAX" ] && PARAMS="$PARAMS --handlerCountMax=$JENKINS_HANDLER_MAX"
[ -n "$JENKINS_HANDLER_IDLE" ] && PARAMS="$PARAMS --handlerCountMaxIdle=$JENKINS_HANDLER_IDLE"
[ -n "$JENKINS_ARGS" ] && PARAMS="$PARAMS $JENKINS_ARGS"
if [ "$JENKINS_ENABLE_ACCESS_LOG" = "yes" ]; then

    # Here i changed access log directory

    PARAMS="$PARAMS --accessLoggerClassName=winstone.accesslog.SimpleAccessLogger --simpleAccessLogger.format=combined --simpleAccessLogger.file=/var/log/jenkins_admin/access_log"
fi
RETVAL=0

case "$1" in
    start)
        echo -n "Starting Jenkins "
        daemon --user "$JENKINS_USER" --pidfile "$JENKINS_PID_FILE" $JAVA_CMD $PARAMS > /dev/null
        RETVAL=$?
        if [ $RETVAL = 0 ]; then
            success
            echo > "$JENKINS_PID_FILE"  # just in case we fail to find it
            MY_SESSION_ID=`/bin/ps h -o sess -p $$`
            # get PID
            /bin/ps hww -u "$JENKINS_USER" -o sess,ppid,pid,cmd | \
            while read sess ppid pid cmd; do
                [ "$ppid" = 1 ] || continue
                # this test doesn't work because Jenkins sets a new Session ID
                # [ "$sess" = "$MY_SESSION_ID" ] || continue
                echo "$cmd" | grep $JENKINS_WAR > /dev/null
                [ $? = 0 ] || continue
                # found a PID
                echo $pid > "$JENKINS_PID_FILE"
            done
            touch $JENKINS_LOCKFILE
        else
            failure
        fi
        echo
        ;;
    stop)
        echo -n "Shutting down Jenkins "

        # Here i changed killproc target

        killproc jenkins_admin
        rm -f $JENKINS_LOCKFILE
        RETVAL=$?
        echo
        ;;
    try-restart|condrestart)
        if test "$1" = "condrestart"; then
                echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
        fi
        $0 status
        if test $? = 0; then
                $0 restart
        else
                : # Not running is not a failure.
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    force-reload)
        echo -n "Reload service Jenkins "
        $0 try-restart
        ;;
    reload)
        $0 restart
        ;;
    status)
        status jenkins
        RETVAL=$?
        ;;
    probe)
        ## Optional: Probe for the necessity of a reload, print out the
        ## argument to this init script which is required for a reload.
        ## Note: probe is not (yet) part of LSB (as of 1.9)
        test "$JENKINS_CONFIG" -nt "$JENKINS_PID_FILE" && echo reload
        ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
        exit 1
        ;;
esac

exit $RETVAL

您能帮我修改脚本以获得正确的停止/重启命令吗?

  • 操作系统:CentOS7
  • Jenkins:最新(来自官方 rpm 仓库)

答案1

我认为你漏掉了一些修改。有很多参考资料$0。这就是脚本本身的名称正如它的名字*。您最好有一个名为 的脚本jenkins和一个名为 的脚本jenkins_admin,与您的命名一致:

# Provides: jenkins_admin

您还错过了:

status)
    status jenkins

该行可能会产生误导性的结果,应该是 jenkins_admin。

我建议声明一个变量,例如:JENKINS_SVC=jenkins_admin并在整个过程中引用该变量,而不是到处重命名。

分辨率也存在问题PID

/bin/ps hww -u "$JENKINS_USER" -o sess,ppid,pid,cmd

PID可能会导致写入错误JENKINS_PID_FILE。此行之前有一条注释,内容为:

echo > "$JENKINS_PID_FILE" # just in case we fail to find it,这将在找出原因之前擦除文件内容。您可以启动两个实例,看看两个文件是否具有相同的PID

您可以通过以下几种方法修复此问题:

  • 添加一个 grep 来匹配 process_name (但 jenkins 也匹配 jenkins_admin)
  • 在不同的用户(jenkins 和 jenkins_admin)下运行每个实例
  • 将两个名称更改为 jenkins_dev 和 jenkins_admin

*$0是“所调用”脚本的名称,因此$0可以解析为:/path/to/jenkins./jenkins或者仅仅是jenkins,取决于它是如何被调用的。

相关内容