centos65上将jenkins(jar包运行)设为daemon service

Jenkins是一个可扩展的持续集成引擎,在测试中用处很大。
公司用jenkins好久了,在centos65上安装的,用一个sh文件启动,以下是具体命令

#!/bin/bash
java -Xms1024m -Xmx2048m -XX:ParallelGCThreads=20 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Dsvnkit.http.sslProtocols=SSLv3 -DHUDSON_HOME=hudson_home -jar jenkins.war --logfile=hudson.log --httpPort=9999 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20

由于安装jenkins的机器是台虚拟机,用vsphere管理,有时候断电容易导致,机器重启。
或者安装插件,更新jenkins等需要重启。每次必须手动启动,感觉比较麻烦。所以就上网搜了一下,
将jenkins设为daemon service,机器启动,jenkins自动自动。具体方法如下:
注意:以下脚本中需要一些参数,比如JENKINS_USER,JENKINS_HOME等,可以将这些参数放到/etc/sysconfig/jenkins文件里,这样你要是用本文的脚本的话,就不用一个个改了,放到这个文件里会自动覆盖脚本里的变量值

1. 创建start-jenkins.sh

#!/bin/bash

# import sysconfig settings and set defaults
[ -f /etc/sysconfig/jenkins ] && . /etc/sysconfig/jenkins
[ "${JENKINS_HOME}" == "" ] && 
    JENKINS_HOME=/home/Automation/Public/CIServer
echo JENKINS_HOME: ${JENKINS_HOME}
[ "${JENKINS_LOG}" == "" ] &&
    JENKINS_LOG=/home/Automation/Public/CIServer/hudson.log
[ "${JENKINS_JAVA}" == "" ] &&
    JENKINS_JAVA=/usr/bin/java
[ "${JENKINS_JAVAOPTS}" == "" ] &&
    JENKINS_JAVAOPTS="-Xms1024m -Xmx2048m -XX:ParallelGCThreads=20 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Dsvnkit.http.sslProtocols=SSLv3 -DHUDSON_HOME=/home/Automation/Public/CIServer/hudson_home"
[ "${JENKINS_IP}" == "" ] &&
    JENKINS_IP=0.0.0.0
[ "${JENKINS_PORT}" == "" ] &&
    JENKINS_PORT=9999
[ "${JENKINS_ARGS}" == "" ] &&
    JENKINS_ARGS=""

JENKINS_WAR=${JENKINS_HOME}/jenkins.war
# check for config errors
JENKINS_ERRORS=()
[ ! -f ${JENKINS_WAR} ] &&
    JENKINS_ERRORS[${#JENKINS_ERRORS[*]}]="JENKINS_HOME : The jenkins.war
could not be found at ${JENKINS_HOME}/jenkins.war"
[ ! -f $JENKINS_JAVA ] &&
    JENKINS_ERRORS[${#JENKINS_ERRORS[*]}]="JENKINS_JAVA : The java
executable could not be found at $JENKINS_JAVA"

# display errors if there are any, otherwise start the process
if [ ${#JENKINS_ERRORS[*]} != '0' ]
then
    echo "CONFIGURATION ERROR:"
    echo "    The following errors occurred when starting Jenkins."
    echo "    Please set the appropriate values at /etc/sysconfig/jenkins"
    echo ""
    for (( i=0; i<${#JENKINS_ERRORS[*]}; i++ ))
    do
        echo "${JENKINS_ERRORS[${i}]}"
    done
    echo ""
    exit 1
else
    echo "starting service"
    echo "nohup nice $JENKINS_JAVA $JENKINS_JAVAOPTS -jar $JENKINS_WAR --httpPort=$JENKINS_PORT --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20 > $JENKINS_LOG 2>&1 &"
    nohup nice $JENKINS_JAVA $JENKINS_JAVAOPTS -jar $JENKINS_WAR --httpPort=$JENKINS_PORT --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20 > $JENKINS_LOG 2>&1 &
fi

该文件应放置在JENKINS_HOME文件夹下,以上脚本可以看出我的JENKINS_HOME值是JENKINS_HOME=/home/Automation/Public/CIServer,所以文件start-jenkins.sh放在/home/Automation/Public/CIServer下面。
在上述脚本中,用到nohub来让jenkins在后台运行。

nohup nice $JENKINS_JAVA $JENKINS_JAVAOPTS -jar $JENKINS_WAR --httpPort=$JENKINS_PORT --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20 > $JENKINS_LOG 2>&1 &

在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会用 & 在程序结尾来让程序自动运行。比如我们要运行mysql在后台: /usr/local/mysql/bin/mysqld_safe –user=mysql &。可是有很多程序并不想mysqld一样,这样我们就需要nohup命令,怎样使用nohup命令呢?这里讲解nohup命令的一些用法。
该命令的一般形式为:

nohup command &

如果使用nohup命令提交作业,那么在缺省情况下该作业的所有输出都被重定向到一个名为nohup.out的文件中,除非另外指定了输出文件:

nohup command > myout.file 2>&1 &

在上面的例子中,输出被重定向到myout.file文件中。
关于nohup的详细信息请参照:http://www.cnblogs.com/allenblogs/archive/2011/05/19/2051136.html

2. 创建stop-jenkins.sh

#!/bin/bash
kill `ps -ef | grep [j]enkins.war | awk '{ print $2 }'`

同样的该文件也需要放置在JENKINS_HOME文件夹下。

3. 创建/etc/init.d/jenkins

#!/bin/bash
# chkconfig: 2345 90 10
# description: Jenkins Continuous Integration server
# processname: /home/Automation/Public/CIServer/jenkins.war

# Source function library.
. /etc/rc.d/init.d/functions

# Get network sysconfig.
. /etc/sysconfig/network

# Check that networking is up, otherwise we can't start
[ "${NETWORKING}" = "no" ] && exit 0

# Get the Jenkins sysconfig
[ -f /etc/sysconfig/jenkins ] && . /etc/sysconfig/jenkins
[ "${JENKINS_HOME}" = "" ] &&
    JENKINS_HOME=/home/Automation/Public/CIServer
[ "${JENKINS_USER}" == "" ] &&
    JENKINS_USER=Automation

startup=${JENKINS_HOME}/start-jenkins.sh
shutdown=${JENKINS_HOME}/stop-jenkins.sh
export JAVA_HOME=/usr/local/jdk1.7

start(){
    echo -n $"Starting Jenkins service: "
    pid=`ps -ef | grep [j]enkins.war | wc -l`
    if [ $pid -gt 0 ]; then
        echo "Jenkins is already running"
        exit 1
    fi
    su - $JENKINS_USER -c $startup
    RETVAL=$?
    [ $RETVAL == 0 ] &&
        echo "Jenkins was started successfully." ||
        echo "There was an error starting Jenkins."
}

stop(){
    action $"Stopping Jenkins service: "
    pid=`ps -ef | grep [j]enkins.war | wc -l`
    if [ ! $pid -gt 0 ]; then
        echo "Jenkins is not running"
        exit 1
    fi
    su - $JENKINS_USER -c $shutdown
    RETVAL=$?
    [ $RETVAL == 0 ] &&
        echo "Jenkins was stopped successfully." ||
        echo "There was an error stopping Jenkins."
}

status(){
    pid=`ps -ef | grep [j]enkins.war | wc -l`
    if [ $pid -gt 0 ]; then
        echo "Jenkins is running..."
    else
        echo "Jenkins is stopped..."
    fi
}

restart(){
    stop
    sleep 5
    start
}

# Call functions as determined by args.
case "$1" in
start)
    start;;
stop)
    stop;;
status)
    status;;
restart)
    restart;;
*)
    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1
esac

exit 0

4.添加权限

#将JENKINS_HOME目录下的所有文件与子目录进行相同的拥有者(JENKINS_USER:Automation)变更(即以递回的方式逐个变更)
#关于chown的用法,参照http://blog.csdn.net/leo201592/article/details/2156083

chown -R Automation. /home/Automation/Public/CIServer

#给三个sh文件加可执行权限,a 表示“所有(all)用户”。它是系统默认值。
#关于chmod具体用法,参照http://wenku.baidu.com/link?url=4NsCX5jpkgroMCYfCuOtp5N-EHNraI_1O35hk__5xK6FyLAezvQre8RHZwEzejH5K-AdUOeayCpKKXPNx9_TWaDVdS07tu7vqCaZYmoK68i

chmod a+x /home/Automation/Public/CIServer/start-jenkins.sh
chmod a+x /home/Automation/Public/CIServer/stop-jenkins.sh
chmod a+x /etc/init.d/jenkins

5.启动jenkins service,并设置为daemon service

service jenkins status
service jenkins start
service jenkins restart
service jenkins stop

#开机启动用以下命令设置,需要在jenkins服务开启的状态下设置
chkconfig jenkins on

#使用以下命令来查看jenkins的运行级别,chkconfig 功能说明:检查,设置系统的各种服务
chkconfig --list | grep jenkins

#结果
jenkins         0:off   1:off   2:on    3:on    4:on    5:on    6:off

#使用命令设置在级别6上,jenkins启动
chkconfig --level 6 jenkins on

chkconfig --list | grep jenkins

#结果
jenkins         0:off   1:off   2:on    3:on    4:on    5:on    6:on

等级0表示:表示关机
等级1表示:单用户模式
等级2表示:无网络连接的多用户命令行模式
等级3表示:有网络连接的多用户命令行模式
等级4表示:不可用
等级5表示:带图形界面的多用户模式
等级6表示:重新启动
关于chkconfig,参照http://baike.baidu.com/link?url=rRAsQo-l5VKjqYXmIe4NhElvWnFlZ9EpEN3janYi7akRLGDe3-AEKgQ4QtoNGWf-4KlSb3XB3vt_e-HgCk85o_

整个设置参照:http://www.justinsilver.com/technology/linux/install-jenkins-centos-service/

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qiyueqinglian/article/details/50699008
文章标签: jenkins centos jar
个人分类: Jenkins
想对作者说点什么? 我来说一句

Jenkins运行

2015年12月08日 60.71MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭