Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

$ 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

    def run (self):

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):
    while name.name!='':

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

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 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

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 restart.py 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
up vote 2 down vote accepted

I managed to kill it using

pkill -f restart.py

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 restart.py" | grep -v grep | awk '{print $2}'`
$ kill -9 `ps aux | grep "0 python restart.py" | 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.