Processing of Golang signal signal

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

After we have modified the configuration file in the actual project, we want to reload the configuration file without restarting the process, then we need to process it by signal passing. The processing of signal in Golang mainly uses two methods in the Os/signal package: One is the Notify method used to listen to the received signal; One is the stop method used to cancel the listener. Here's a few examples.

Monitoring signal

Notify Method prototypes

Func Notify (c chan<-os. Signal, sig ... os. Signal)

The first parameter represents the pipeline that receives the signal
The second and subsequent parameters indicate the setting of the signal to be monitored, if not set to indicate that all signals are monitored.

Package Mainimport ("FMT", "OS" "os/signal"//"Syscall") func main () {c: = make (chan os). Signal) Signal. Notify (c)//monitor the specified signal//signal. Notify (c, Syscall. SIGHUP, Syscall. SIGUSR2)//block until a signal is passed in s: = <-cfmt. PRINTLN ("Get signal:", s)}

After running the program, the program's process number is searched and the kill command is run to see the output of the signal.

Terminal 1:

The go run main.go//is in a blocking state when Terminal 2 performs a kill and outputs the following information get Signal:user defined signal 2

Terminal 2:

$ PS-EF | grep main501 839 543 0 11:16pm ttys000 0:00.03 go run main.go501 842 839 0 11:16pm ttys000 0:00.00/var/folders/s8/kbqz28x d3wl8q5rw8vsr129c0000gn/t/go-build384577908/command-line-arguments/_obj/exe/main501 844 709 0 11:16PM ttys001 0:00.00 grep main$ KILL-USR2 842

Of course, such a program in practice is not useful, improve it can be used to receive signals and processing some content.

Package Mainimport ("FMT", "OS" "Os/signal" "Syscall" "Time") Func main () {go Signallisten () for {time. Sleep (Ten * time. Second)}}func Signallisten () {c: = make (chan os). Signal) Signal. Notify (c, Syscall. SIGUSR2) for {s: = <-c//after receiving signal processing, here is just the output signal content, can do something more interesting fmt. PRINTLN ("Get signal:", s)}}

The main thing is to open a gorutine alone to handle the signal monitoring.

Stop listening

Package Mainimport ("FMT", "OS" "Os/signal" "Syscall") func main () {c: = Make (chan os. Signal) Signal. Notify (c, Syscall. SIGUSR2)//When the method is called, the <-c receives a signal in the following for loop and exits. Signal. Stop (c) for {s: = <-cfmt. PRINTLN ("Get signal:", s)}}

Summary

The signal processing in the Golang is very simple, but there is much more to know about the signal itself, and it is recommended that you refer to the signal chapter in Advanced UNIX programming.

Reprint Please specify: Happy programming»golang signal signal processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.