Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

The following is a Python application which spans a few threads, then spawns a new process of itself and exits:

$ cat
import os
import random
import signal
import sys 
import threading
import time

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

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

    def run (self):*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

    print("Time to die!")
    t = CallThreads(restart)
    t.daemon = True

def provider_query(name):

def restart():

def signal_handler(signal, frame):

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

When I hit ^C I do get a bash prompt, but the output still comes and I still see the script in the process table:

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

I've tried killing it with kill 5751 && kill 5752, but that doesn't help even if I'm fast enough to do so before the PID changes (on a new process when the script restarts). I've tried pkill but that does not help either. I'm wary of using pkill python as there are other Python processes running that I don't want to kill. Even closing the Konsole window in which the script is running does not help!

How can I kill the script?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

I managed to kill it using

pkill -f

From the man page:

   -f     The pattern is normally only matched against the process name.
          When -f is set, the full command line is used.
share|improve this answer
Thank you, this is a much cleaner solution than mine! – dotancohen Aug 1 '13 at 10:14

I could only kill it by running these two commands quickly one after the other a few times (with two taps on the up arrow key):

$ kill -9 `ps aux | grep "sh -c python" | grep -v grep | awk '{print $2}'`
$ kill -9 `ps aux | grep "0 python" | grep -v grep | awk '{print $2}'`

What a pain!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.