I am currently using a script to call pkill to terminate my C++ program.
However i noticed that the destructors were not called from my traces when using pkill.
Is there another good way that i can exit the program gracefully?
pkill seems kind of untidy and some logs in the buffer do not get recorded. I’d like to be able to flush on my fstream and to close all resources programatically (instead of relying on the O/S to clean up my mess).
The application runs 24/7 without any problem, the only time i want to stop it is during maintenance. The application does not have any user interface for me to type exit.
You do it by defining a signal handler for
SIGTERMalong these lines:Somewhere in your include block:
Yes, we’re doing i
Cstyle!Somewhere in the initialization part of your code:
and then define the signal handlers code (flush everything, etc):
Now when you run
pkill <app>, where<app>is the name of the executable, the code forhandler()will run.Without switches, the default
SIGTERMsignal will be sent to the application. Should you choose to use a different signal you would have to make sure you send the same signal as you “catch” in thehandler().Relevant information can be found by
man 7 signaland of course,man kill.