V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Deteriorator
V2EX  ›  Python

Python 中怎么使用 Linux 杀死进程的命令?

  •  
  •   Deteriorator · Feb 27, 2019 · 4849 views
    This topic created in 2617 days ago, the information mentioned may be changed or developed.

    我的代码中有这样一条下发杀死进程的命令:

    self._linux.cmd("ps aux | grep '{}' | awk {print '$2'} | xargs kill".format(process_name),
                            timeout=timeout)
    

    但是在调试的时候,发现一直有一个报错

    >>> linux.linux.kill_process_by_name_in_pc('ftp')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/root/linux_utils_lib.py", line 37, in kill_process_by_name_in_pc
        self._linux.cmd("ps aux | grep '{}' | awk {print '$2'} | xargs kill".format(process_name),
    KeyError: 'print $2'
    

    麻烦大佬看看,不知道怎么使用

    16 replies    2019-03-07 14:05:45 +08:00
    ysc3839
        1
    ysc3839  
       Feb 27, 2019 via Android
    {print '$2'} 被当成了 format 的占位符。
    atonganan
        2
    atonganan  
       Feb 27, 2019
    直接用 + + 做连接符,不用格式化
    Deteriorator
        3
    Deteriorator  
    OP
       Feb 27, 2019
    @ysc3839 我把它改成字符串拼接的形式还是不行,还是有报错,请问怎么解决?
    self._linux.cmd("ps aux | grep '" + process_name + "' | awk {print '$2'} | xargs kill",
    timeout=timeout)

    >>> linux.linux.kill_process_by_name_in_pc('ftp')
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/root/linux_utils_lib.py", line 37, in kill_process_by_name_in_pc
    self._linux.cmd("ps aux | grep '" + process_name + "' | awk {print '$2'} | xargs kill",
    KeyError: 'print $2'
    blackeeper
        4
    blackeeper  
       Feb 27, 2019
    awk 后面用单引号,这个样子:awk '{print $2}'
    ysc3839
        5
    ysc3839  
       Feb 27, 2019 via Android
    @Deteriorator 这我就不知道了。看看 cmd 这个函数吧。
    ETiV
        6
    ETiV  
       Feb 27, 2019 via iPhone
    一般都有 pgrep、pkill 命令,就别 ps | grep 了
    wlsnx
        7
    wlsnx  
       Feb 27, 2019
    In [1]: "ps aux | grep '{}' | awk {print '$2'} | xargs kill".format("a")
    ---------------------------------------------------------------------------
    KeyError Traceback (most recent call last)
    <ipython-input-1-a6604b2f5478> in <module>
    ----> 1 "ps aux | grep '{}' | awk {print '$2'} | xargs kill".format("a")

    KeyError: "print '$2'"

    In [2]: "ps aux | grep '{}' | awk {{print '$2'}} | xargs kill".format("a")
    Out[2]: "ps aux | grep 'a' | awk {print '$2'} | xargs kill"
    KasuganoSoras
        8
    KasuganoSoras  
       Feb 27, 2019
    请使用 pkill 进程名
    lynskylate
        10
    lynskylate  
       Feb 27, 2019 via Android
    awk 的{}和 format 冲突了,另外一般调命令也是用 subprocess
    hhhsuan
        11
    hhhsuan  
       Feb 27, 2019 via Android
    Python 可以用 psutil 这个库杀进程,很方面,还能兼容 Linux 和 Windows
    Sylv
        12
    Sylv  
       Feb 27, 2019 via iPhone
    {{print '$2'}}
    lfzyx
        13
    lfzyx  
       Feb 27, 2019
    看到这种代码吓尿了,请用 psutil 或者 os.kill(pid, sig)
    Deteriorator
        14
    Deteriorator  
    OP
       Feb 27, 2019
    @wlsnx
    @atonganan
    @ysc3839
    @blackeeper
    @ETiV
    @KasuganoSoras
    @hcymk2
    @lynskylate
    @hhhsuan
    @Sylv
    @lfzyx 感谢各位慷慨指点,我把 awk 后的 print 和$2 之间的空格去掉后,解决了这个问题,使我的代码可以正常运行,虽然没有使用各位推荐的方法,但还是谢谢各位,谢谢
    mythmgn
        15
    mythmgn  
       Mar 5, 2019
    @Deteriorator

    1. 最好养成习惯把 grep 进程 filter 掉

    ps -ef|grep xxx|grep -v grep

    2. 如果是个 baselib, 最好还把一些 vim emacs vi tail more less 常见的编辑器也 grep -v filter 掉


    防止误杀.
    Deteriorator
        16
    Deteriorator  
    OP
       Mar 7, 2019
    @mythmgn 多谢提醒
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4858 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 75ms · UTC 09:57 · PVG 17:57 · LAX 02:57 · JFK 05:57
    ♥ Do have faith in what you're doing.