转载:https://blog.csdn.net/yaolanshu_june/article/details/79176523 

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""
  fi

  根据自己的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=Jenkins
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/src/jenkins/jenkins.sh start
ExecReload=
ExecStop=/usr/local/src/jenkins/jenkins.sh stop
PrivateTmp=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 重启测试查看

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐