看到Kite里面的源码,有段代码挺有意思。
上代码先:
// SetupSignalHandler listens to signals and toggles the log level to DEBUG // mode when it received a SIGUSR2 signal. Another SIGUSR2 toggles the log // level back to the old level. func (k *Kite) SetupSignalHandler() { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGUSR2) go func() { for s := range c { k.Log.Info("Got signal: %s", s) if debugMode { // toogle back to old settings. k.Log.Info("Disabling debug mode") if k.SetLogLevel == nil { k.Log.Error("SetLogLevel is not defined") continue } k.SetLogLevel(getLogLevel()) debugMode = false } else { k.Log.Info("Enabling debug mode") if k.SetLogLevel == nil { k.Log.Error("SetLogLevel is not defined") continue } k.SetLogLevel(DEBUG) debugMode = true } } }() }
可以通过给正在运行的程序,切换日志模式。
举一反三,可以利用这个方式,来做很多开关切换。
--------------------补充--------------
SIGUSR的code的查看和发送方式//Ubuntu
cat /usr/include/asm-generic/signal.h | grep SIGUS #define SIGUSR1 10 #define SIGUSR2 12
//Mac
grep -r "SIGUS" /usr/include/
/usr/include//apache2/ap_mpm.h: signals for its use (i.e. SIGUSR1), but guarantees that these signals
/usr/include//apache2/mpm_common.h:#define AP_SIG_GRACEFUL SIGUSR1
/usr/include//apache2/mpm_common.h:#define AP_SIG_GRACEFUL_STRING "SIGUSR1"
/usr/include//sys/signal.h:#define SIGUSR1 30 /* user defined signal 1 */
/usr/include//sys/signal.h:#define SIGUSR2 31 /* user defined signal 2 */
kill -12 [PID]