V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2019-02-27 11:27:59 +08:00 · 4186 次点击
    这是一个创建于 1856 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    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 条回复    2019-03-07 14:05:45 +08:00
    ysc3839
        1
    ysc3839  
       2019-02-27 11:31:14 +08:00 via Android
    {print '$2'} 被当成了 format 的占位符。
    atonganan
        2
    atonganan  
       2019-02-27 11:33:46 +08:00
    直接用 + + 做连接符,不用格式化
    Deteriorator
        3
    Deteriorator  
    OP
       2019-02-27 11:36:07 +08:00
    @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  
       2019-02-27 11:49:36 +08:00
    awk 后面用单引号,这个样子:awk '{print $2}'
    ysc3839
        5
    ysc3839  
       2019-02-27 11:52:34 +08:00 via Android
    @Deteriorator 这我就不知道了。看看 cmd 这个函数吧。
    ETiV
        6
    ETiV  
       2019-02-27 11:57:00 +08:00 via iPhone
    一般都有 pgrep、pkill 命令,就别 ps | grep 了
    wlsnx
        7
    wlsnx  
       2019-02-27 12:05:35 +08:00
    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  
       2019-02-27 12:10:11 +08:00
    请使用 pkill 进程名
    hcymk2
        9
    hcymk2  
       2019-02-27 12:28:43 +08:00
    lynskylate
        10
    lynskylate  
       2019-02-27 12:31:33 +08:00 via Android
    awk 的{}和 format 冲突了,另外一般调命令也是用 subprocess
    hhhsuan
        11
    hhhsuan  
       2019-02-27 12:31:46 +08:00 via Android
    Python 可以用 psutil 这个库杀进程,很方面,还能兼容 Linux 和 Windows
    Sylv
        12
    Sylv  
       2019-02-27 12:43:01 +08:00 via iPhone
    {{print '$2'}}
    lfzyx
        13
    lfzyx  
       2019-02-27 13:12:52 +08:00
    看到这种代码吓尿了,请用 psutil 或者 os.kill(pid, sig)
    Deteriorator
        14
    Deteriorator  
    OP
       2019-02-27 13:27:51 +08:00
    @wlsnx
    @atonganan
    @ysc3839
    @blackeeper
    @ETiV
    @KasuganoSoras
    @hcymk2
    @lynskylate
    @hhhsuan
    @Sylv
    @lfzyx 感谢各位慷慨指点,我把 awk 后的 print 和$2 之间的空格去掉后,解决了这个问题,使我的代码可以正常运行,虽然没有使用各位推荐的方法,但还是谢谢各位,谢谢
    mythmgn
        15
    mythmgn  
       2019-03-05 10:40:17 +08:00
    @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
       2019-03-07 14:05:45 +08:00
    @mythmgn 多谢提醒
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3317 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 73ms · UTC 13:46 · PVG 21:46 · LAX 06:46 · JFK 09:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.