Help us shape the future of Go! Take the 2026 Go Developer Survey

gsignal

package module
v0.0.0-...-d4d0acc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2016 License: MIT Imports: 5 Imported by: 0

README

GSignal

Golang library for monitoring asynchronously signals.

Getting Started

$ go get github.com/bluele/gsignal

Examples

package main

import (
  "github.com/bluele/gsignal"
  "log"
  "os"
  "syscall"
  "time"
)

func main() {
  watcher := gsignal.NewWatcher()
  watcher.Watch(func(sig os.Signal) {
    log.Println("Catch signal: ", sig)
    os.Exit(0)
  }, os.Interrupt, syscall.SIGTERM)

  watcher.Watch(func(sig os.Signal) {
    log.Println("Ignore signal: ", sig)
  }, syscall.SIGHUP)

  // start watching specified signal numbers.
  watcher.Run()

  log.Println("PID: ", os.Getpid())
  log.Println("Sleep...")
  time.Sleep(time.Minute)
}
send Ctr^C to this process.
$ go run example.go
2015/01/04 14:30:40 PID:  96311 # your process id.
2015/01/04 14:30:40 Sleep...
^C2015/01/04 14:30:41 Catch signal:  interrupt
send SIGTERM to this process.
$ kill -SIGTERM <your pid>
2015/01/04 14:30:40 PID:  96311 # your process id.
2015/01/04 14:30:40 Sleep...
^C2015/01/04 14:30:41 Catch signal:  terminated
send SIGHUP to this process.
$ kill -SIGHUP <your pid>
2015/01/04 14:30:40 PID:  96311 # your process id.
2015/01/04 14:30:40 Sleep...
^C2015/01/04 14:30:41 Ignore signal:  hangup

Testing

$ go test

Author

Jun Kimura

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alarm

func Alarm(delay time.Duration)

send SIGALRM to this process.

func SendSignal

func SendSignal(delay time.Duration, sigs ...syscall.Signal)

delay: delay for sending signal. sigs: signal numbers send specified signal to this process.

Types

type CallbackHandler

type CallbackHandler func(os.Signal)

type for callback handler

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

watcher object

func NewWatcher

func NewWatcher() *Watcher

Create a new signal watcher.

func (*Watcher) GetCallback

func (gs *Watcher) GetCallback(sig os.Signal) (CallbackHandler, bool)

get callback handler of specified signal and boolean value for exists callback.

func (*Watcher) Reload

func (gs *Watcher) Reload()

reload watching signals.

func (*Watcher) Run

func (gs *Watcher) Run()

start watching specified signals asynchronously.

func (*Watcher) Stop

func (gs *Watcher) Stop()

stop watching signals.

func (*Watcher) UnWatch

func (gs *Watcher) UnWatch(signals ...os.Signal) *Watcher

signals: signals to unmonitor.

func (*Watcher) Watch

func (gs *Watcher) Watch(cb CallbackHandler, signals ...os.Signal) *Watcher

cb: callback hander when watcher catches specified signals. signals: signals to monitor.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL