Linux开机启动Jenkins

jenkins默认开机启动,首先编写开机启动脚本,将脚本放在/etc/init.d/目录下,脚本名称Jenkins,存放Jenkins的tomcat在/data/目录下
创建脚本:
[root@CentOS-jenkins ~]# vim /etc/init.d/jenkins
脚本内容:

#! /bin/bash
#chkconfig:345 80 80      
#345是安全级别,80 80 是启动响应优先级 
#description:Jenkins
#开机启动时描述

#Jenkins的存放位置,文件夹名称是tomcat-jenkins(后面会用到)
jenkinspath=/data/tomcat-jenkins/

#加载函数库
. /etc/init.d/functions
#判断路径是否存在
if [ ! -d "$jenkinspath" ] ; then
    echo "cannot find jenkins path"
    exit 0
fi

if (($# != 1)); then
    echo "Usage:$0 {start|stop|restart}"
    exit 0
fi

case "$1" in
  status)
    status=`ps -ef |grep -v grep |grep tomcat-jenkins |wc -l`
    if(($status==1));then
        action "jenkins is running" /bin/true
        exit 0
    fi
    if(($status==0));then
        action "jenkins is not running" /bin/false
        exit 0
    fi
    ;;    
  start)
    status=`ps -ef |grep -v grep |grep tomcat-jenkins |wc -l`
    if(($status==1));then    
        echo "jenkins has been started"
        exit 0
    fi    
    sh  $jenkinspath/bin/startup.sh >& /dev/null 
    [ $? -eq 0 ] && action "jenkins is starting" /bin/true ||\
    action "jenkins start failure" /bin/false
    ;;
  stop)
    status=`ps -ef |grep -v grep |grep tomcat-jenkins |wc -l`
    if(($status==0));then
        echo "jenkins has been stopped"
        exit 0
    fi
    ps -ef |grep -v grep |grep tomcat-jenkins |awk '{print $2}'|xargs kill -9
    [ $? -eq 0 ]&& action "jenkins is stopping" /bin/true ||\
    action "jenkins stop failure" /bin/false
    ;;
  restart)
    status=`ps -ef |grep -v grep |grep tomcat-jenkins |wc -l`
    if(($status==1));then
                ps -ef |grep -v grep |grep tomcat-jenkins |awk '{print $2}'|xargs kill -9
    fi
    sh  $jenkinspath/bin/startup.sh >& /dev/null
    [ $? -eq 0 ]&& action "jenkins is restarting" /bin/true ||\
    action "jenkins restart failure" /bin/false
    ;;
  *)
    echo "Usage:$0 {start|stop|restart}"
    exit
    ;;
esac

给脚本赋予权限:
[root@CentOS-jenkins ~]# chmod u+x /etc/init.d/jenkins
添加到chkconfig列表:
[root@CentOS-jenkins ~]# chkconfig --add jenkins
设置开机启动:
[root@CentOS-jenkins ~]# chkconfig --level 2345 jenkins on

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐