2

I am trying to write a python script that also deals killing/stopping its own process with the signals.

It runs each files one at a time, sleep at specific time and run again until it finished the whole directory with files. The processing time of each file is around 5 to 10 minutes depending on the size.

However, I want my program to stop when I give the signal. It should not kill it right away. It should run the current file and stop afterwards.

So I cannot use CTRL Z because it suspends the pid right away.

stop = False
def handler(number, frame):
    global stop
    stop = True
signal.signal(signal.SIGUSR1, handler)
while not stop:
    # Do things

Above is what I tried, but it kills it right away when I signal. Also it goes into an infinite loop even after it finishes working on all the files.

What can I do to stop the process when I signal, allowing it to finish processing the current file first?

2
  • 1
    Have you tried to catch SIGTERM instead SIGUSR1? Commented Mar 17, 2015 at 18:25
  • I suspect the infinite loop depends on the condition you put in your while statement. If you do not send the USR1 signal, there is nothing telling your program to exit the loop. The for loop (with the break inside) proposed by jsbueno in his answer is a better solution. Commented Mar 19, 2015 at 8:53

2 Answers 2

1

Just install a signal handler for signal.SIGTERM - and within it setup a state variable in your program that you check when finishing processing each file.

It is actually quite simple - see the documentation at: https://docs.python.org/2/library/signal.html .

import os 
import signal
terminate = False

for filename in os.listdir("<your dir>"):
    if terminate:
       break
    process_next_file(filename)

def handler(signum, frame):
    global terminate
    print("Termination requested")
    terminate = True

signal.signal(signal.SIGTERM, handler)

(Also, you can use other signals - SIGINT is the one used when the user press ctrl+C for example)

Sign up to request clarification or add additional context in comments.

3 Comments

isnt this exactly same as what I did? this goes into the infinite loop and kills it right away instead of waiting for a file to finish
Yes . sorry - this is the same you did but for using "SIGTERM". But on the test I did here, it did work. As for the infinite loop, I will update the answer
how do I kill it using your coding? is it kill -SIGTERM PID?
1

You can create a command listener thread. Main thread still does file processing. For example, the listener thread waits a command from standard input. When you send "stop" command, it sets a varible. The file processor thread checks the variable before processing a file. So, it can stop when you want to stop processing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.