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

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

jenkins.sh

#!/bin/bash
###主要目的用于开机启动服务,不然 启动jenkins.war包没有java -jar的权限
JAVA_HOME=/usr/local/src/jdk1.8.0_192
 
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/src/jenkins/jenkins.war --httpPort=8081  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""

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

###启动jenkins

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

 ###停止jenkins

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

###让jenkins有可执行权限

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

2.配置开机启动服务

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

[Unit]Description=JenkinsAfter=network.target [Service]Type=forkingExecStart=/usr/local/src/jenkins/jenkins.sh startExecReload=ExecStop=/usr/local/src/jenkins/jenkins.sh stopPrivateTmp=true [Install]WantedBy=multi-user.target

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

systemctl daemon-reload

2.3启动脚本

systemctl start jenkins.service

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

systemctl status jenkins.service

2.5设置开机启动

systemctl enable jenkins.service

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

systemctl list-units --type=service

2.7停止服务命令

systemctl stop jenkins.service

2.8 reboot 重启测试查看