如何杀死启动新进程的脚本?

以下是一个跨越几个线程的Python应用程序,然后生成一个自己的新进程并退出:

$cat restart.py
import os
import random
import signal
import sys 
import threading
import time


class Name(object):
    def __init__(self, name):
        self.name = name


class CallThreads(threading.Thread):
    def __init__(self, target, *args):
        self.target = target
        self.args = args
        threading.Thread.__init__(self)

    def run (self):
        self.target(*self.args)


def main(args):
    print("Hello, world!")
    letter = random.choice(['A', 'B', 'C', 'D', 'E', 'F'])
    count = 0 
    while count<3:
        count += 1
        name = Name(letter+str(count))
        t = CallThreads(provider_query, name)
        t.daemon = True
        t.start()
        time.sleep(3)
        print("------------")

    print("Time to die!")
    t = CallThreads(restart)
    t.daemon = True
    t.start()
    time.sleep(0.1)
    sys.exit(0)


def provider_query(name):
    while name.name!='':
        print(name.name)
        time.sleep(1)


def restart():
    os.system('python restart.py')


def signal_handler(signal, frame):
    sys.exit()


if __name__ == '__main__':
    signal.signal(signal.SIGINT, signal_handler)
    main(sys.argv)

当我点击^ C时,我确实得到了一个bash提示符,但输出仍然存在,我仍然看到进程表中的脚本:

$ps aux | grep restart.py
1000      5751  0.0  0.0   4396   616 pts/3    S    08:41   0:00 sh -c python restart.py
1000      5752  0.3  0.1 253184  5724 pts/3    Sl   08:41   0:00 python restart.py
1000      5786  0.0  0.0   9388   936 pts/4    S+   08:41   0:00 grep --color=auto restart.py

我试过杀死它5751&&杀死5752,但即使我在PID更改之前足够快(在脚本重新启动时的新进程上),这也无济于事.我试过pkill restart.py,但这也无济于事.我担心使用pkill python,因为还有其他运行的Python进程,我不想杀死它.即使关闭运行脚本的Konsole窗口也无济于事!

我怎么能杀死剧本?

最佳答案
我设法用它来杀死它

pkill -f restart.py

从手册页:

   -f     The pattern is normally only matched against the process name.
          When -f is set, the full command line is used.

转载注明原文:如何杀死启动新进程的脚本? - 代码日志