Sample script to trap signals in Python

Purpose

Study signals and leave a note of the script that traps SIGTERM

reference

I referred to here https://engineeringnote.hateblo.jp/entry/python/signal

script

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

behavior


python signale.py 
Waiting for signal...
Waiting for signal...

Check the process

ps aux | grep test
ec2-user 21269  0.0  0.1 275896  9144 pts/1    Sl+  16:40   0:00 python36 test.py

Issuance of SIGTERM

kill -SIGTERM 21269

Confirm that it was trapped

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

Sample script to trap signals in Python
Sample to put Python Kivy in one file
To flush stdout in Python
Login to website in Python
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
Preparing to script control Rhino objects in Grasshopper / Python
Speech to speech in python [text to speech]
How to develop in Python
Post to Slack in Python
Script to count and stop up to 5 seconds in Python in Blender
Assigned scaffolding macro in Python script file to F12 key
How to sample from any probability density function in Python
Convert markdown to PDF in Python
How to collect images in Python
How to use SQLite in Python
Get the script path in Python
In the python command python points to python3.8
[python] Copy script to generate copy log
Try to calculate Trace in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
How to handle Japanese in Python
An alternative to `pause` in Python
How to pass arguments to a Python script in SPSS Modeler Batch
I wrote a function to load a Git extension script in Python
I wrote a script to extract a web page link in Python
Articles to read when Blender Python script code doesn't work in 2.80
Python script written in PyTorch is converted to exe with PyInstaller
I tried to implement PLSA in Python
[Introduction to Python] How to use class in Python?
Try logging in to qiita with Python
Install Pyaudio to play wave in python
I tried to implement permutation in Python
Method to build Python environment in Xcode 6
How to dynamically define variables in Python
How to do R chartr () in Python
Send email to multiple recipients in Python (Python 3)
I tried to implement PLSA in Python 2
To set default encoding to utf-8 in python
Decorator to avoid UnicodeEncodeError in Python 3 print ()
How to work with BigQuery in Python
Log in to Slack using requests in Python
Run the Python interpreter in a script
How to get a stacktrace in python
How to display multiplication table in python
Easy way to use Wikipedia in Python
How to extract polygon area in Python
How to check opencv version in python
I tried to implement ADALINE in Python
[Python] Pandas to fully understand in 10 minutes
Throw Incoming Webhooks to Mattermost in Python
Module to generate word N-gram in Python
To reference environment variables in Python in Blender
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
Sample for handling eml files in Python
How to switch python versions in cloud9