jenkins - Jenkins 停止/启动系统服务
问题描述
大家好,在我的 Jenkins 管道中,我需要停止系统服务,复制最新文件,然后再次重新启动服务。我被困在停止系统服务步骤。
操作系统是 CentOS,Jenkins 在用户 'jenkins' 下运行。
按照这篇文章中接受的答案(https://serverfault.com/questions/772778/allowing-a-non-root-user-to-restart-a-service)我创建了一个新组,将“jenkins”添加到该组中然后通过“visudo”更新 sudo 列表。现在用户 jenkins 可以像这样在 Putty 命令行上停止/启动该服务:sudo systemctl stop <my-service>
当我像下面这样更新管道文件时
sh "echo stop Linux service"
sh "sudo systemctl stop <my-service>"
我收到以下错误:
+ echo stop Linux service
stop Linux service
[Pipeline] sh
+ sudo systemctl stop <my-service>
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
sudo: no tty present and no askpass program specified
如果我在管道中删除 sudo,则会收到此错误:
+ echo stop Linux service
stop Linux service
[Pipeline] sh
+ systemctl stop <my-service>
Failed to stop <my-service>: Interactive authentication required.
See system logs and 'systemctl status <my-service>' for details.
请问我怎样才能实现我的目标?
解决方案
将用户添加到 sudoers 组仅允许他们调用sudo
. 他们仍然需要使用密码进行身份验证。由于您不是在 TTY 上运行此命令,因此无法询问您的密码,因此 sudo 会说“Nuh uh”。
那么,显而易见的解决方案是确保您的 Jenkins 代理用户根本不需要密码来调用 sudo。您可以通过NOPASSWD
在/etc/sudoers
.
运行visudo
,并将行设置为:
jenkins ALL=(ALL) NOPASSWD: ALL
推荐阅读
- node.js - 输入选项的值
- mysql - 如何解决 django.db.utils.DataError: (1406, "Data too long for column 'product_team' at row 1")
- nuxt.js - 如何避免 Nuxt 中的页面模板重复
- spring-integration - 有什么方法可以通过 spring 集成获得流状态(可能像运行、停止、停止异常)?
- javascript - 页面重新加载后使用 Firestore 对 Angular 进行数据持久性
- asp.net-web-api - ASP.NET WebApi2 OData 处理带有斜线的查询 /
- java - Project Reactor 有条件地执行独立验证步骤
- php - PHP 从文件中读取对象属性值
- javascript - 在redux angular 9中使用异步管道和#select装饰器时出现无限错误
- python - 使用 API(图形 API、Youtube API)在 facebook、instagram 和 Youtube 上关注用户