Study signals and leave a note of the script that traps SIGTERM
I referred to here https://engineeringnote.hateblo.jp/entry/python/signal
signal.py
import signal
import time
from threading import Thread
FLAG = False
def handler(signum, frame):
global FLAG
print("signal={}".format(signum))
FLAG = True
def waiting():
global FLAG
while not FLAG:
print("Waiting for signal...")
time.sleep(5)
t1 = Thread(target=waiting, args=[])
t2 = Thread(target=waiting, args=[])
t1.start()
t2.start()
signal.signal(signal.SIGTERM, handler)
signal.signal(signal.SIGINT, handler)
signal.pause()
python signale.py
Waiting for signal...
Waiting for signal...
ps aux | grep test
ec2-user 21269 0.0 0.1 275896 9144 pts/1 Sl+ 16:40 0:00 python36 test.py
kill -SIGTERM 21269
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
Waiting for signal...
signal=15
Recommended Posts