【发布时间】:2023-08-28 00:06:01
【问题描述】:
我在远程 Ubuntu 服务器上有一个脚本。我尝试在 jenkins 构建成功后执行脚本,但错误如下:
sudo: no tty present and no askpass program specified
配置如下,
谁能帮帮我? 谢谢。
【问题讨论】:
我在远程 Ubuntu 服务器上有一个脚本。我尝试在 jenkins 构建成功后执行脚本,但错误如下:
sudo: no tty present and no askpass program specified
配置如下,
谁能帮帮我? 谢谢。
【问题讨论】:
问题是您的脚本在某些时候使用了sudo。通常的方法是将需要您使用 sudo 的脚本添加到 sudoers。
示例:在您的脚本中使用sudo service apache2 reload,现在创建一个包含该行的 bash 脚本并将该脚本添加到 sudoers 文件中。
新脚本名称:/home/quaser/restart-apache.sh
使用:visudo
在文件底部添加:
jenkins ALL=(ALL) NOPASSWD: /home/quaser/restart-apache.sh
现在,在您的脚本中更改:sudo service apache restart 到 sudo /home/quaser/restart-apache.sh,并且不应要求您输入密码。
【讨论】:
我遇到了同样的问题,我通过在 /etc/sudoers 上评论 Defaults requiretty 解决了这个问题
cat /etc/sudoers| grep tty
#Defaults requiretty
来自手册页:
man sudoers | grep requiretty -A 5
requiretty If set, sudo will only run when the user is logged in
to a real tty. When this flag is set, sudo can only be
run from a login session and not via other means such
as cron(8) or cgi-bin scripts. This flag is off by
default.
【讨论】: