Jenkins服务开机自启动

2023-08-11,,

最近因为护网行动,每天都要对服务器进行开、关机操作。为了省事儿,对Jenkins服务进行开机自动启动服务改造。实现如下:

1. 通过chkconfig --list命令列出系统中已安装的服务及其启动状态

[root@qy-ggyf-zyl-32 ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'. celeryd-Server_soinnx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@qy-ggyf-zyl-32 ~]#

2. 创建启动脚本

  在/etc/init.d/目录下,创建Jenkins启动脚本

vim jenkins_startup.sh

  脚本内容如下:

#!/bin/bash
#chkconfig:2345 80 90
#decription:启动Jenkins nohup /usr/local/jdk-11.0.17/bin/java -jar /usr/local/jenkins.war &
#请将/usr/local/jdk-11.0.17/bin/java和usr/local/jenkins.war替换为实际的路径。

3. 编辑完脚本后对脚本进行赋权

 chmod +x jenkins_startup.sh

4. 将脚本添加进清单

chkconfig --add jenkins_startup.sh

5. 添加完毕后查看清单

[root@qy-ggyf-zyl-32 ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'. celeryd-Server_soinnx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
jenkins_startup.sh 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@qy-ggyf-zyl-32 ~]#

至此,完成了Jenkins随操作系统启动自启服务的操作,也可以使用service jenkins_startup.sh start/stop/restart命令来重启服务。


问题记录及调试过程:

① 最初的脚本内容:

#!/bin/bash
#chkconfig:2345 80 90
#decription:启动Jenkins nohup java -jar /usr/local/jenkins.war &

② Java设置了环境变量,但是开机启动Jenkins服务失败,手动调试也出现报错,如下:

[root@qy-ggyf-zyl-32 init.d]# service jenkins_startup.sh restart
[root@qy-ggyf-zyl-32 init.d]# nohup: appending output to 'nohup.out'
nohup: failed to run command 'java': No such file or directory

③ 查看Java路径:

[root@qy-ggyf-zyl-32 init.d]# which java
/usr/local/jdk-11.0.17/bin/java

④ 将jenkins_startup.sh的Java路径改写为绝对路径:/usr/local/jdk-11.0.17/bin/java,再次执行service jenkins_startup.sh restart,服务重启成功,Jenkins服务也实现了开机自启动。

[root@qy-ggyf-zyl-32 init.d]# service jenkins_startup.sh restart
[root@qy-ggyf-zyl-32 init.d]# nohup: appending output to 'nohup.out'

Jenkins服务开机自启动的相关教程结束。

《Jenkins服务开机自启动.doc》

下载本文的Word格式文档,以方便收藏与打印。