如何在python中按名称杀死进程

时间:2017-05-03 12:25:41

标签: python python-2.7

>>> os.system('adb shell pidof logcat')

750 4774

0
>>> os.system('adb shell ps | grep logcat')

root      750   1     8760   1720  __skb_recv 7f8f5a5edc S /system/bin/logcat

root      4774  4681  8708   1696  __skb_recv 7f98efeedc S logcat

logcat有2个进程。 如何杀死这两个进程ID:750 4774

3 个答案:

答案 0 :(得分:4)

您可以尝试psutil

import psutil

PROC_NAME = "abc.exe"

for proc in psutil.process_iter():
    # check whether the process to kill name matches
    if proc.name() == PROC_NAME:
        proc.kill()

答案 1 :(得分:2)

<?php

if( have_rows('insert_breed_name_to_add') ){
    $count = 0;
    while( have_rows('insert_breed_name_to_add') ){

        the_row();

        // If it starts from A or B or C
        if(get_sub_field('breed_name')[0] == 'A' || get_sub_field('breed_name')[0] == 'B' || get_sub_field('breed_name')[0] == 'C'){

                $count++;
                // If it is first time create "col-4"
                if($count == 1){ ?>
                <div class="col-4"><ul>
                <?php } ?>

                <!-- Create Li -->
                <li><?php the_sub_field('breed_name'); ?></li>

                 <!-- close col-4 -->
                <?php if ($count%3 == 0){?>
                    </ul>
                </div>
                <?php }


        }
    }
}

?>

如果找不到taskkill.exe,则可能需要使用import subprocess subprocess.call(['taskkill.exe', '/IM', 'logcat'])

答案 2 :(得分:0)

介绍我喜欢的kill.py

import psutil

command_substring_to_purge = "app.py"
pids_to_kill = []
processes = psutil.process_iter(attrs=["pid", "cmdline"])

for process in processes:
    pid = process.info["pid"]
    for subcommands in process.info["cmdline"]:
        if command_substring_to_purge in subcommands:
            pids_to_kill.append(pid)
            break

for pid in pids_to_kill:
    process = psutil.Process(pid)
    process.terminate()

请注意,要杀死root程序,您需要以root身份运行它。
强大的力量伴随着巨大的责任。