I have an http server (launched using http.Handle
) and I would like to do some operations.
How can I do that (on linux) ? Is it possible to do those operations in case of a ctrl-C ?
I'm not familiar with unix signals so the answer may be trivial.
I have an http server (launched using http.Handle
) and I would like to do some operations.
How can I do that (on linux) ? Is it possible to do those operations in case of a ctrl-C ?
I'm not familiar with unix signals so the answer may be trivial.
I suppose author is interested not only in Ctrl+C and offer more broad solution for Linux (for Windows signals see x/sys/windows):
P.S. None of signals handles
os.Exit
.With this configuration on Ctrl+C or on receiving other signal program will push
os.Signal
into channelexitChan
which will unblock<-exitChan
operation and themain
function will continue execution on the final lines, then return, then execute deferred functions.Non-Main Deferreds
For non-main deferred you may:
You can subscribe to the TERM and INT signals using the signal package. But note that these signals are only sent when the process is killed explicitly; normal exit (initiated by the process itself) does not involve any sort of signals. I think for normal exit just do something in the main routine (which supposedly should spawn worker goroutines and then wait on them).
Read
man 7 signal
for more general info on POSIX signals.Using kostix answer, I built this code (now adapted to Go1) to catch the interrupt signal and do some operations before exiting :