I have a python script that runs in a docker container. The script executes properly but after the execution the container crashed. In order to avoid the crashing I copied the following script from another question:
import signal
import time
class GracefulKiller:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self,signum, frame):
self.kill_now = True
if __name__ == '__main__':
killer = GracefulKiller()
while True:
time.sleep(1)
print("doing something in a loop ...")
if killer.kill_now:
break
print("End of the program. I was killed gracefully")
I would replace the line with “do something in a loop” with my actualy code later but I kept it for testing purposes. The “do something in a loop” is printed multiple times before it says it was killed gracefully but right after it crashes again. I want the actual code to be executed only once and then gracefully end the script without crashing. What should I do?
Error message:
2017-06-15 14:11:55 [APP/PROC/WEB/0] OUT doing something in a loop ...
2017-06-15 14:11:55 [APP/PROC/WEB/0] OUT doing something in a loop ...
2017-06-15 14:11:55 [APP/PROC/WEB/0] OUT doing something in a loop ...
2017-06-15 14:11:55 [APP/PROC/WEB/0] OUT doing something in a loop ...
2017-06-15 14:11:55 [APP/PROC/WEB/0] OUT doing something in a loop ...
2017-06-15 14:11:55 [APP/PROC/WEB/0] OUT doing something in a loop ...
2017-06-15 14:11:55 [APP/PROC/WEB/0] OUT doing something in a loop ...
2017-06-15 14:11:55 [APP/PROC/WEB/0] OUT End of the program. I was killed gracefully
2017-06-15 14:11:55 [APP/PROC/WEB/0] OUT Exit status 0
2017-06-15 14:11:55 [API/1] OUT Process has crashed with type: "web"
2017-06-15 14:11:55 [API/1] OUT App instance exited with guid 2e34ae4a-648a-4284-83aa-bf44f284ccf5 payload: {"instance"=>"", "index"=>0, "reason"=>"CRASHED", "exit_description"=>"2 error(s) occurred:\n\n* 1 error(s) occurred:\n\n* Exited with status 4\n* 2 error(s) occurred:\n\n* cancelled\n* cancelled", "crash_count"=>1, "crash_timestamp"=>1497528715620049488, "version"=>"1c721e51-4ab8-4aa6-b1fe-f5654cd83f2c"}
2017-06-15 14:11:55 [CELL/0] OUT Destroying container