You can kill a process by
corrupt the file, if you are sending RPC messages, you will break the process in between and drop all messages.
To handle signal you can use
- https://gist.github.com/alirezastack/ae4e12a21ccb91264b69e1d14a53c044
The above method will handle
To test it you can run the script in terminal and then try to find the pid number of the process and finally kill it:
The above command will issue
#python #sigint #sigterm #signal #kill
kill
command. But have you thought what would happen to the process which is runing if you issue kill command? It will do something nasty in between if you do not handle the kill signal gracefully. If you are writing to a file, it willcorrupt the file, if you are sending RPC messages, you will break the process in between and drop all messages.
To handle signal you can use
signal
python module. A sample of the signal handling is created as a gist in github below:- https://gist.github.com/alirezastack/ae4e12a21ccb91264b69e1d14a53c044
The above method will handle
SIGINT
, SIGTERM
and end the loop gracefully.To test it you can run the script in terminal and then try to find the pid number of the process and finally kill it:
sudo kill 4773
The above command will issue
SIGTERM
and script will handle it gracefully. SIGINT
on the other hand is issued when you press CTRL+C
.#python #sigint #sigterm #signal #kill
Gist
Gracefully kill python script using signal module