linux(centos8):用systemctl管理war包形式的jenkins(java 14 / jenkins 2.257)

一,如何安装jenkins?

参见:

https://www.cnblogs.com/architectforest/p/13685904.html

 

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

 

二,创建管理脚本

1,把下载好的jenkins的war包移动到软件的安装目录

[root@kubemaster jenkins]# mkdir /usr/local/soft/jenkins
[root@kubemaster jenkins]# mv jenkins.war /usr/local/soft/jenkins/

 

2,在jenkins目录下创建脚本:

[root@kubemaster jenkins]# cd /usr/local/soft/jenkins/
[root@kubemaster jenkins]# ls
jenkins.war
[root@kubemaster jenkins]# vi jenkins.sh
[root@kubemaster jenkins]# chmod +x jenkins.sh

脚本内容:

[root@kubemaster jenkins]# more jenkins.sh 
#!/bin/bash
JAVA_HOME=/usr/local/soft/jdk-14.0.2

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
      nohup $JAVA_HOME/bin/java -jar /usr/local/soft/jenkins/jenkins.war --enable-future-java --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

 

三,测试管理脚本的效果

1,启动:

[root@kubemaster jenkins]# ./jenkins.sh start
nohup: 把输出追加到'nohup.out'

查看端口:

[root@kubemaster jenkins]# ss -lntp
State    Recv-Q  Send-Q  Local Address:Port  Peer Address:Port                                                                      
...     
LISTEN   0       50            *:8081            *:*    users:(("java",pid=8412,fd=139))

可以看到8081这个端口已在工作中

 

2,访问:

http://127.0.0.1:8081/

返回:

 

 登录访问即可

 

3,停止服务:

[root@kubemaster jenkins]# ./jenkins.sh stop
已杀死

 

四,创建systemd的service服务文件

1,生成service文件

[root@kubemaster jenkins]# vi /lib/systemd/system/jenkins.service

service内容:

[root@kubemaster jenkins]# more /lib/systemd/system/jenkins.service
[Unit]
Description=Jenkins
After=network.target

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

[Install]
WantedBy=multi-user.target

 

五,用sytemctl使service生效:

重新加载service

[root@kubemaster jenkins]# systemctl daemon-reload

启动:

[root@kubemaster jenkins]# systemctl start jenkins

查看状态:

[root@kubemaster jenkins]# systemctl status jenkins
● jenkins.service - Jenkins
   Loaded: loaded (/usr/lib/systemd/system/jenkins.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-09-17 19:14:05 CST; 1min 9s ago
  Process: 9182 ExecStop=/usr/local/soft/jenkins/jenkins.sh stop (code=killed, signal=KILL)
  Process: 9235 ExecStart=/usr/local/soft/jenkins/jenkins.sh start (code=exited, status=0/SUCCESS)
 Main PID: 9242 (java)
    Tasks: 48 (limit: 23797)
   Memory: 273.2M
   CGroup: /system.slice/jenkins.service
           └─9242 /usr/local/soft/jdk-14.0.2/bin/java -jar /usr/local/soft/jenkins/jenkins.war --enable-future-java --httpPort=8081

9月 17 19:14:10 kubemaster jenkins.sh[9235]: 2020-09-17 11:14:10.820+0000 [id=44]        INFO     

关闭:

[root@kubemaster jenkins]# systemctl stop jenkins
已杀死

如需让jenkins自启动:

[root@kubemaster jenkins]# systemctl enable jenkins

 

六,查看java的版本

[root@kubemaster jenkins]# java --version
java 14.0.2 2020-07-14
Java(TM) SE Runtime Environment (build 14.0.2+12-46)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)

 

七,查看jenkins版本

在登录后首页的右下角,可以看到当前的版本:2.257

八,查看linux版本:

[root@kubemaster jenkins]# cat /etc/redhat-release 
CentOS Linux release 8.2.2004 (Core) 

 

posted @ 2020-09-17 19:19  刘宏缔的架构森林  阅读(524)  评论(0编辑  收藏  举报