[英]using python subprocess.call to kill all running python files
[英]Using Python's subprocess.call to kill firefox process
我試圖使用下面的腳本殺死在我的系統上運行的任何firefox進程作為python腳本的一部分:
if subprocess.call( [ "killall -9 firefox-bin" ] ) is not 0:
self._logger.debug( 'Firefox cleanup - FAILURE!' )
else:
self._logger.debug( 'Firefox cleanup - SUCCESS!' )
我遇到如下所示的以下錯誤,但是只要我在終端中直接使用它而沒有任何錯誤,“killall -9 firefox-bin”就可以工作。
Traceback (most recent call last):
File "./pythonfile", line 109, in __runMethod
if subprocess.call( [ "killall -9 firefox-bin" ] ) is not 0:
File "/usr/lib/python2.6/subprocess.py", line 478, in call
p = Popen(*popenargs, **kwargs)
File "/usr/lib/python2.6/subprocess.py", line 639, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我錯過了什么或者我應該嘗試使用不同的python模塊嗎?
使用subprocess.call
時需要分隔參數:
if subprocess.call( [ "killall", "-9", "firefox-bin" ] ) > 0:
self._logger.debug( 'Firefox cleanup - FAILURE!' )
else:
self._logger.debug( 'Firefox cleanup - SUCCESS!' )
call()
通常不像shell那樣處理你的命令,也不會將它解析為單獨的參數。 有關完整說明,請參閱常用參數 。
如果必須依賴shell解析命令,請將shell
關鍵字參數設置為True
:
if subprocess.call( "killall -9 firefox-bin", shell=True ) > 0:
self._logger.debug( 'Firefox cleanup - FAILURE!' )
else:
self._logger.debug( 'Firefox cleanup - SUCCESS!' )
請注意,我將測試更改為> 0
以更清楚地了解可能的返回值。 由於Python解釋器中的實現細節, is
測試恰好適用於小整數,但不是測試整數相等性的正確方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.