首页 > 解决方案 > 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.

请问我怎样才能实现我的目标?

标签: jenkinscentos7

解决方案


将用户添加到 sudoers 组仅允许他们调用sudo. 他们仍然需要使用密码进行身份验证。由于您不是在 TTY 上运行此命令,因此无法询问您的密码,因此 sudo 会说“Nuh uh”。

那么,显而易见的解决方案是确保您的 Jenkins 代理用户根本不需要密码来调用 sudo。您可以通过NOPASSWD/etc/sudoers.

运行visudo,并将行设置为:

jenkins ALL=(ALL) NOPASSWD: ALL

推荐阅读