I want to capture the Ctrl+C (SIGINT
) signal sent from the console and print out some partial run totals.
Is this possible in Golang?
Note: When I first posted the question I was confused about Ctrl+C being SIGTERM
instead of SIGINT
.
I want to capture the Ctrl+C (SIGINT
) signal sent from the console and print out some partial run totals.
Is this possible in Golang?
Note: When I first posted the question I was confused about Ctrl+C being SIGTERM
instead of SIGINT
.
This works:
Just for the record if somebody needs a way to handle signals on Windows. I had a requirement to handle from prog A calling prog B through os/exec but prog B never was able to terminate gracefully because sending signals through ex. cmd.Process.Signal(syscall.SIGTERM) or other signals are not supported on Windows. The way I handled is by creating a temp file as a signal ex. .signal.term through prog A and prog B needs to check if that file exists on interval base, if file exists it will exit the program and handle a cleanup if needed, I'm sure there are other ways but this did the job.
This is another version which work in case you have some tasks to cleanup. Code will leave clean up process in their method.
in case you need to clean main function you need to capture signal in main thread using go func() as well.