【问题标题】:How can I kill the explorer.exe process?如何杀死 explorer.exe 进程?
【发布时间】:2015-12-19 11:15:01
【问题描述】:

我正在编写一个旨在杀死 explorer.exe 的脚本。我对其进行了一些搜索,我看到的最佳答案使用taskkill 命令。我试过了,但是当我在我的电脑上运行它时,它说它可以工作,但实际上并没有杀死它。

import os, socket

s = socket.socket()
host = socket.gethostname()

try:
    s.bind((host, 75))
except socket.error:
    print 'Error: Premission denied, please run as admin'
    exit()

s.listen(5)

while True:
    print '[*] Deploying Server on: ' + host
    print '[*] Scanning..'
    c, addr = s.accept()
    print '[*] Connection established from ' + str(addr)
    while True:
        try:
            os.system("taskkill /im explorer.exe")
            cmd = raw_input()
            if cmd == 'exit':
                print '[!] Exiting'
                c.send('exit')
                s.close()
                exit()
            c.send(cmd)
        except KeyboardInterrupt:
            print '[!] Exiting'
            c.send('exit')
            s.close()
            exit()

有效载荷:

import os
import socket
import platform

print 'Starting'
system = platform.system()
windows = ['Microsoft', 'Windows']
s = socket.socket()
host = socket.gethostname()
print host
print platform.system()
try:
    s.connect((host, 75))
except socket.error:
    s.close()
    s.connect((host, 75))
while True:
    cmd = s.recv(1024)
    if cmd == 'exit':
        s.close()
        exit()
    os.system("taskkill /im explorer.exe")
    print(os.system("taskkill /im explorer.exe"))

【问题讨论】:

  • 你确定它根本没有被杀死吗?我并不总是杀死 explorer.exe,但当我这样做时,我更喜欢它的默认行为,即立即自行重启。
  • 好吧,我看到它还在我的电脑上运行,我怎么知道天气是不是马上重启了?
  • taskkill 尝试连接到进程的窗口站和桌面,并将WM_CLOSE 消息发布到主窗口(对于控制台应用程序,它是 conhost.exe 窗口,但前提是只有一个进程连接到控制台)。如果它无法发布消息,它会失败(退出代码为 1)并说您需要使用 /F(强制),这将调用TerminateProcess。仅仅因为它成功地发布了WM_CLOSE 消息,并不意味着程序实际上会关闭。探索者没有重生;它只是忽略窗口消息。您可以使用 /F,但您必须手动重新启动资源管理器。
  • 如果使用 /F,您需要将目标范围缩小到活动的“控制台”会话(不要与控制台窗口混淆)。否则,它可能会杀死所有已登录用户的 explorer.exe 的所有实例。您可以使用csid = ctypes.windll.kernel32.WTSGetActiveConsoleSessionId() 获取控制台会话号。然后将其添加为过滤器,例如os.system('taskkill /f /im explorer.exe /fi "session eq %d"' % csid).
  • 另外,您应该使用subprocess.call 而不是os.system。没有理由使用 cmd.exe shell 运行它。加上子进程,您可以通过creationflags=DETACHED_PROCESS (8) 来运行 taskkill.exe 而无需控制台窗口。这样,如果它是一个 GUI 应用程序,控制台窗口就不会在桌面上短暂闪烁。

标签: python windows python-2.7 taskkill


【解决方案1】:

我建议改用os.kill()。它更清晰,并返回更清晰的值。 你可以这样做:

import wmi

for process in wim.WMI().Win32_Process ():
    if process.Name == 'explorer.exe':
        os.kill(process.ProcessId)

请注意,您运行的 Python 版本很重要 (https://docs.python.org/2/faq/windows.html#how-do-i-emulate-os-kill-in-windows)

【讨论】:

  • Windows 中没有信号,所以os.kill 是用硬TerminateProcess 实现的。您也可以在 WMI process 对象上调用 Terminate 方法。这就像 Unix 上的 SIGKILL,而不是更礼貌的 SIGTERM。 Windows 上的礼貌方式是 PostMessage WM_CLOSE 到窗口或 PostThreadMessage WM_QUIT 到线程。对于控制台应用程序,如果它是附加到控制台的唯一进程,您可以将WM_CLOSE 发布到控制台窗口,这将通过CTRL_CLOSE_EVENT 通知应用程序。
  • 没错,我记得 WMI 中的一个错误,由于某种原因,如果使用 Terminate,它无法杀死多个同名进程。但我相信它已被修复。总之,process.Terminate() 也是个好主意。
【解决方案2】:

我遇到了同样的问题,我需要终止 explorer.exe 进程。显然你应该强制用/F标志杀死进程。

os.system("taskkill /im explorer.exe /F")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 2013-07-24
    相关资源
    最近更新 更多