【问题标题】:Running a linux command from python从 python 运行 linux 命令
【发布时间】:2016-01-03 03:41:12
【问题描述】:

我需要从 python 运行这个 linux 命令并将输出分配给一个变量。

ps -ef | grep rtptransmit | grep -v grep

我已经尝试使用 pythons 命令库来做到这一点。

import commands
a = commands.getoutput('ps -ef | grep rtptransmit | grep -v grep')

但是 a 得到了 cut off 的结束。我得到的输出是:

'nvr      20714 20711  0 10:39 ?        00:00:00 /opt/americandynamics/venvr/bin/rtptransmit setup_req db=media  camera=6  stream=video  substream=1  client_a'

但预期的输出是:

nvr      20714 20711  0 10:39 ?        00:00:00 /opt/americandynamics/venvr/bin/rtptransmit setup_req db=media  camera=6  stream=video  substream=1  client_address=192.168.200.179  client_rtp_port=6970  override_lockout=1  clienttype=1

有谁知道如何阻止输出被切断,或者任何人都可以建议另一种方法吗?

【问题讨论】:

    标签: python linux


    【解决方案1】:
    nano test.py
    
    import os
    a = os.system('ps -ef | grep rtptransmit | grep -v grep')
    print(a)
    
    python test.py
    
    python3 test.py
    

    Run python file using both python and python3

    Run python script using python and python3

    【讨论】:

    • 能否请您添加有关此解决方案如何解决问题的更多信息?
    • 以下是How do I write a good answer? 的一些指南。这个提供的答案可能是正确的,但它可以从解释中受益。
    【解决方案2】:
    #!/usr/bin/python
    import os
    
    a = os.system("cat /var/log/syslog")
    
    print a
    
    
    from subprocess import call
    
    b = call("ls -l", shell=True)
    
    print b
    
    
    import subprocess
    
    cmd = subprocess.check_output('ps -ef | grep kernel', shell=True)
    
    print cmd
    

    上述任何脚本都适合你:-)

    【讨论】:

      【解决方案3】:

      ps 显然将其输出限制为适合终端的假定宽度。您可以使用$COLUMNS 环境变量或--columns 选项覆盖此宽度ps

      commands 模块已弃用。使用subprocess 获取ps -ef 的输出,并在Python 中过滤输出。不要像其他答案所建议的那样使用shell=True,在这种情况下它只是多余的:

      ps = subprocess.Popen(['ps', '-ef', '--columns', '1000'], stdout=subprocess.PIPE)
      output = ps.communicate()[0]
      for line in output.splitlines():
          if 'rtptransmit' in line:
              print(line)
      

      您可能还想查看pgrep 命令,您可以通过该命令直接搜索特定进程。

      【讨论】:

        【解决方案4】:

        commands 已弃用,您不应使用它。请改用subprocess

        import subprocess
        a = subprocess.check_output('ps -ef | grep rtptransmit | grep -v grep', shell=True)
        

        【讨论】:

        • @lunaryorn:问题是如何在 Python 中运行该命令,而不是如何在 Python 中 grep。
        • 实际上问题是如何避免截断ps 输出。但这并不与我的观点相矛盾……
        • @lunaryorn:另外,没有shell=True 的外壳管道将无法工作,所以你也错了。
        • 那又怎样?关键是在这种情况下你不需要壳管……
        • 有很多方法可以得到ps 的输出,从坏的到好的。但是,如果未设置 pscolumn 宽度,则输出将被截断,这就是问题和答案的重点。因此,认为没有必要使用 shell 和管道是愚蠢的,因为这不是重点。
        【解决方案5】:

        我通常使用subprocess 来运行外部命令。对于您的情况,您可以执行以下操作

        from subprocess import Popen, PIPE
        
        p = Popen('ps -ef | grep rtptransmit | grep -v grep', shell=True,
                  stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        

        输出将在out 变量中。

        【讨论】:

        • 如果要在命令中使用管道,则需要shell。关于grep,我实际上只是复制并粘贴了问题中的命令。我怀疑第二个grep 在那里,因为有时我们执行的grep 命令也似乎是grep-ed,因此需要删除。这实际上可以避免使用grep [r]tptransmit
        • 我知道在命令中使用管道需要shell=True,第二个grep是从过滤的进程列表中删除第一个grep命令。但在这种情况下,管道和grep 都是多余的。
        • @lunaryorn 情况更糟。 Python代码在我的环境中产生截断的输出:似乎如果$COLUMNS被隐式设置,那么ps会忽略它,例如,输出(在shell中)比shutil.get_terminal_size()os.environ['COLUMNS']长,但会引发KeyError但@987654337 @ 对应于shutil.get_terminal_size()
        猜你喜欢
        • 2015-06-03
        • 2015-12-19
        • 2018-05-04
        • 1970-01-01
        • 2018-02-16
        • 2020-07-06
        • 2012-02-01
        • 2011-01-10
        相关资源
        最近更新 更多