1.启动 jenkins 脚本

​jenkins.sh​

#!/bin/bash
#主要目的用于开机启动服务,不然 启动jenkins.war包没有java -jar的>权限
JAVA_HOME=/usr/local/jdk1.8.0_201


pid=`ps -ef | grep jenkins.war | grep -v 'grep'| awk '{print $2}'| wc -l`
if [ "$1" = "start" ];then
if [ $pid -gt 0 ];then
echo 'jenkins is running...'
else
#java启动服务 配置java安装根路径,和启动war包存的根路径
nohup $JAVA_HOME/bin/java -jar /usr/local/jenkins/jenkins.war>jenkins.log --httpPort=8080 2>&1 &
fi
elif [ "$1" = "stop" ];then
exec ps -ef | grep jenkins | grep -v grep | awk '{print $2}'| xargs kill -9
echo 'jenkins is stop..'
else
echo "Please input like this:"./jenkins.sh start" or "./jenkins stop""
fi

注意:在写shell脚本时,开头一定要加上​​#!/bin/bash​​,否则执行该脚本会报错

根据自己的java安装目录,和jenkins.war包存放目录来修改脚本,我的脚本放在/usr/local/jenkins/目录下

2.让jenkins有可执行权限

chmod +x /usr/local/jenkins/jenkins.sh

补充:

启动jenkins

/usr/local/src/jenkins/jenkins.sh start

停止jenkins

/usr/local/src/jenkins/jenkins.sh stop

3.配置开机启动服务

3.1 到 /lib/systemd/system 服务注册目录下创建 jenkins.service

[Unit]
Description=Jenkins
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/jenkins/jenkins.sh start
ExecReload=
ExecStop=/usr/local/jenkins/jenkins.sh stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

执行路径根据实际情况进行修改

3.2创建好服务后,执行一下命令刷新配置

systemctl daemon-reload

3.3启动脚本

systemctl start jenkins.service

3.4查看启动脚本状态是否启用成功(失败的话,看错误日志进行修改)

失败时,查看日志:​​journalctl -xe​

systemctl status jenkins.service

三、Centos 7 Linux创建Jenkins启动脚本以及开机启动服务_java

3.5设置开机启动

systemctl enable jenkins.service

3.6查看设置开机启动的服务列表

systemctl list-units --type=service

三、Centos 7 Linux创建Jenkins启动脚本以及开机启动服务_开机启动_02

3.7停止服务命令

systemctl stop jenkins.service