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?
whilestatement. If you do not send the USR1 signal, there is nothing telling your program to exit the loop. Theforloop (with thebreakinside) proposed by jsbueno in his answer is a better solution.