1. <tfoot id='9cw8B'></tfoot>
      <i id='9cw8B'><tr id='9cw8B'><dt id='9cw8B'><q id='9cw8B'><span id='9cw8B'><b id='9cw8B'><form id='9cw8B'><ins id='9cw8B'></ins><ul id='9cw8B'></ul><sub id='9cw8B'></sub></form><legend id='9cw8B'></legend><bdo id='9cw8B'><pre id='9cw8B'><center id='9cw8B'></center></pre></bdo></b><th id='9cw8B'></th></span></q></dt></tr></i><div id='9cw8B'><tfoot id='9cw8B'></tfoot><dl id='9cw8B'><fieldset id='9cw8B'></fieldset></dl></div>
        <legend id='9cw8B'><style id='9cw8B'><dir id='9cw8B'><q id='9cw8B'></q></dir></style></legend>

        <small id='9cw8B'></small><noframes id='9cw8B'>

        • <bdo id='9cw8B'></bdo><ul id='9cw8B'></ul>

        从 python 脚本以超级用户身份运行命令

        running a command as a super user from a python script(从 python 脚本以超级用户身份运行命令)
        • <tfoot id='UR0ij'></tfoot>

          <small id='UR0ij'></small><noframes id='UR0ij'>

            • <legend id='UR0ij'><style id='UR0ij'><dir id='UR0ij'><q id='UR0ij'></q></dir></style></legend>
              1. <i id='UR0ij'><tr id='UR0ij'><dt id='UR0ij'><q id='UR0ij'><span id='UR0ij'><b id='UR0ij'><form id='UR0ij'><ins id='UR0ij'></ins><ul id='UR0ij'></ul><sub id='UR0ij'></sub></form><legend id='UR0ij'></legend><bdo id='UR0ij'><pre id='UR0ij'><center id='UR0ij'></center></pre></bdo></b><th id='UR0ij'></th></span></q></dt></tr></i><div id='UR0ij'><tfoot id='UR0ij'></tfoot><dl id='UR0ij'><fieldset id='UR0ij'></fieldset></dl></div>
                  <bdo id='UR0ij'></bdo><ul id='UR0ij'></ul>
                    <tbody id='UR0ij'></tbody>
                  本文介绍了从 python 脚本以超级用户身份运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我正在尝试使用子进程从 python 脚本中以超级用户身份运行一个进程.在 ipython shell 中类似于

                  So I'm trying to get a process to be run as a super user from within a python script using subprocess. In the ipython shell something like

                  proc = subprocess.Popen('sudo apach2ctl restart',
                                          shell=True, stdin=subprocess.PIPE,
                                          stdout=subprocess.PIPE,
                                          stderr=subprocess.PIPE)
                  

                  工作正常,但只要我将它粘贴到脚本中,我就会开始得到:sudo: apach2ctl: command not found.

                  works fine, but as soon as I stick it into a script I start getting: sudo: apach2ctl: command not found.

                  我猜这是由于 sudo 在 ubuntu 上处理环境的方式.(我也试过 sudo -E apche2ctl restartsudo env path=$PATH apache2ctl restart 无济于事)

                  I would guess this is due to the way sudo handles environments on ubuntu. (I've also tried sudo -E apche2ctl restart and sudo env path=$PATH apache2ctl restart with no avail)

                  所以我的问题基本上是,如果我想以超级用户身份运行 apache2ctl restart 并在需要时提示用户输入超级用户密码,我应该怎么做呢?我无意在脚本中存储密码.

                  So my question is basically, if I want to run apache2ctl restart as super user that prompts the user for the super user password when required, how should I go about doing this? I have no intention of storing passwords in the script.

                  我尝试将命令作为字符串传递并标记为列表.在 python 解释器中,我会用一个字符串正确地得到密码提示(在 python 脚本中仍然不能像我原来的问题那样工作),一个列表只是给出了 sudo 的帮助屏幕.

                  I've tried passing in the commands as both a string and tokenized into a list. In the python interpreter, with a string I'll get the password prompt properly (still doesnt work in a python script as in my original problem), a list just gives the help screen for sudo.

                  编辑 2:

                  所以我收集到的是,虽然当 shell=True 时 Popen 会像字符串一样处理一些命令,但它需要

                  So what I gather is that while Popen will work with some commands just as strings when shell=True, it takes

                  proc = subprocess.Popen(['sudo','/usr/sbin/apache2ctl','restart'])
                  

                  没有 'shell=True' 让 sudo 工作.

                  without 'shell=True' to get sudo to work.

                  谢谢!

                  推荐答案

                  尝试给出 apache2ctl 的完整路径.

                  Try giving the full path to apache2ctl.

                  这篇关于从 python 脚本以超级用户身份运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

                    <tfoot id='GQPQj'></tfoot>
                      <bdo id='GQPQj'></bdo><ul id='GQPQj'></ul>
                      <legend id='GQPQj'><style id='GQPQj'><dir id='GQPQj'><q id='GQPQj'></q></dir></style></legend>
                          <tbody id='GQPQj'></tbody>

                          1. <i id='GQPQj'><tr id='GQPQj'><dt id='GQPQj'><q id='GQPQj'><span id='GQPQj'><b id='GQPQj'><form id='GQPQj'><ins id='GQPQj'></ins><ul id='GQPQj'></ul><sub id='GQPQj'></sub></form><legend id='GQPQj'></legend><bdo id='GQPQj'><pre id='GQPQj'><center id='GQPQj'></center></pre></bdo></b><th id='GQPQj'></th></span></q></dt></tr></i><div id='GQPQj'><tfoot id='GQPQj'></tfoot><dl id='GQPQj'><fieldset id='GQPQj'></fieldset></dl></div>

                            <small id='GQPQj'></small><noframes id='GQPQj'>