我需要运行一个需要 sudo 的脚本(text.sh),无需输入密码,例如:
sudo apt-get update >> /home/derp/test.txt
我已经修改了我的 sudoers 文件,如下所示:
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL) ALL
pedro ALL=(ALL) NOPASSWD: /home/pedro/Desktop/scripts/test.sh
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
如果我将该脚本修改为:
time
输出为:
real 0m0.000s
user 0m0.000s
sys 0m0.000s
正如它应该。
我真的不知道还能做什么……
由于我在控制台中收到了密码提示,因此调用了该脚本,因此我认为这不是 Django 或 python 的问题。
------编辑-----显然您需要在 python 调用中指定 sudo,如下所示:
subprocess.call(['sudo', "/your/script.sh"])
你可以把它从脚本中去掉。所以它已经修复了。