本文整理汇总了Golang中os.Signal函数的典型用法代码示例。如果您正苦于以下问题:Golang Signal函数的具体用法?Golang Signal怎么用?Golang Signal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Signal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: termSignalListener
// termSignalListener returns a signal chan that closes when either (a) the process receives a termination
// signal: SIGTERM, SIGINT, or SIGHUP; or (b) the abort chan closes.
func termSignalListener(abort <-chan struct{}) <-chan struct{} {
shouldQuit := make(chan struct{})
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh)
go func() {
defer close(shouldQuit)
for {
select {
case <-abort:
log.Infof("executor died, aborting")
return
case s, ok := <-sigCh:
if !ok {
return
}
switch s {
case os.Interrupt, os.Signal(syscall.SIGTERM), os.Signal(syscall.SIGINT), os.Signal(syscall.SIGHUP):
log.Infof("received signal %q, aborting", s)
return
case os.Signal(syscall.SIGCHLD): // who cares?
default:
log.Errorf("unexpected signal: %T %#v", s, s)
}
}
}
}()
return shouldQuit
}
示例2: closeOnSignal
func (p *Proxy) closeOnSignal() {
ch := make(chan os.Signal, 10)
signal.Notify(ch, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM), os.Signal(syscall.SIGHUP))
sig := <-ch
p.ul.Close()
switch sig {
case os.Signal(syscall.SIGHUP):
log.Printf("graceful shutdown from signal: %v\n", sig)
default:
log.Fatalf("exiting from signal: %v\n", sig)
}
}
示例3: wait
func (h *shutdownHandler) wait() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, os.Signal(syscall.SIGTERM))
sig := <-ch
grohl.Log(grohl.Data{"fn": "shutdown", "at": "start", "signal": fmt.Sprint(sig)})
h.shutdown(nil)
}
示例4: startServer
func startServer(listenOn, dataDir string, pidFile string) {
logger.Info("using version ", GITCOMMIT)
logger.Info("starting server on ", listenOn)
logger.Info("using dataDir ", dataDir)
logger.Info("using pidFile", pidFile)
if err := createPidFile(pidFile); err != nil {
logger.Error(err)
os.Exit(1)
}
defer removePidFile(pidFile)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
go func() {
sig := <-c
logger.Debug("Received signal '%v', exiting\n", sig)
removePidFile(pidFile)
os.Exit(0)
}()
if err := http.ListenAndServe(listenOn, NewHandler(dataDir)); err != nil {
logger.Error(err.Error())
}
}
示例5: jobInitApi
// jobInitApi runs the remote api server `srv` as a daemon,
// Only one api server can run at the same time - this is enforced by a pidfile.
// The signals SIGINT, SIGKILL and SIGTERM are intercepted for cleanup.
func jobInitApi(job *engine.Job) string {
job.Logf("Creating server")
srv, err := NewServer(job.Eng, ConfigFromJob(job))
if err != nil {
return err.Error()
}
if srv.runtime.config.Pidfile != "" {
job.Logf("Creating pidfile")
if err := utils.CreatePidFile(srv.runtime.config.Pidfile); err != nil {
log.Fatal(err)
}
}
job.Logf("Setting up signal traps")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
go func() {
sig := <-c
log.Printf("Received signal '%v', exiting\n", sig)
utils.RemovePidFile(srv.runtime.config.Pidfile)
srv.Close()
os.Exit(0)
}()
job.Eng.Hack_SetGlobalVar("httpapi.server", srv)
if err := job.Eng.Register("create", srv.ContainerCreate); err != nil {
return err.Error()
}
if err := job.Eng.Register("start", srv.ContainerStart); err != nil {
return err.Error()
}
if err := job.Eng.Register("serveapi", srv.ListenAndServe); err != nil {
return err.Error()
}
return "0"
}
示例6: processExistsAtPidString
// Used for checking if a process exists in pidfile
func processExistsAtPidString(pidStr string) bool {
// Check if the process exists
pid, err := strconv.Atoi(pidStr)
if err != nil {
return true // Convert error, can't determine.
}
process, err := os.FindProcess(pid)
if err != nil {
return true
}
err = process.Signal(os.Signal(syscall.Signal(0)))
if err == nil {
return true
}
errno, ok := err.(syscall.Errno)
if !ok {
return false
}
switch errno {
case syscall.ESRCH:
return false // Only here we could be sure the process does not exist.
case syscall.EPERM:
return true
}
return true
}
示例7: daemon
func daemon(pidfile, addr string, port int, autoRestart bool) error {
if addr != "127.0.0.1" {
log.Println("/!\\ DON'T BIND ON ANOTHER IP ADDRESS THAN 127.0.0.1 IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
}
if err := createPidFile(pidfile); err != nil {
log.Fatal(err)
}
defer removePidFile(pidfile)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
go func() {
sig := <-c
log.Printf("Received signal '%v', exiting\n", sig)
removePidFile(pidfile)
os.Exit(0)
}()
server, err := docker.NewServer(autoRestart)
if err != nil {
return err
}
return docker.ListenAndServe(fmt.Sprintf("%s:%d", addr, port), server, true)
}
示例8: stopService
func (s *Service) stopService() error {
s.stopTimer()
s.mu.Lock()
if !s.State.isRunState() {
if s.State.canStop() {
s.infof("stopped")
}
s.State = StateStopped
s.mu.Unlock()
return nil
}
prevState := s.State
s.State = StateStopping
s.infof("stopping")
p := s.Cmd.Process
s.mu.Unlock()
if s != nil {
stopped := isStoppedErr(p.Signal(os.Signal(syscall.SIGTERM)))
if !stopped {
select {
case <-s.stopCh:
stopped = true
case <-time.After(10 * time.Second):
stopped = isStoppedErr(p.Kill())
}
if !stopped {
select {
case <-s.stopCh:
case <-time.After(2 * time.Second):
// sending signal 0 checks that the process is
// alive and we're allowed to send the signal
// without actually sending anything
if isStoppedErr(p.Signal(syscall.Signal(0))) {
break
}
s.mu.Lock()
s.State = prevState
s.mu.Unlock()
err := fmt.Errorf("could not stop, probably stuck")
s.errorf("%v", err)
return err
}
}
}
}
s.mu.Lock()
s.State = StateStopped
s.Restarts = 0
s.mu.Unlock()
if s.Config.Log != nil {
s.Config.Log.Close()
}
s.infof("stopped")
return nil
}
示例9: wait
func (h *shutdownHandler) wait() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, os.Signal(syscall.SIGTERM))
sig := <-ch
grohl.Log(grohl.Data{"fn": "shutdown", "at": "start", "signal": fmt.Sprint(sig)})
// signal exit handlers
close(h.done)
// wait for exit handlers to finish
h.mtx.Lock()
os.Exit(0)
}
示例10: handleSignals
func handleSignals() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc)
for s := range sigc {
switch s {
case os.Interrupt, os.Signal(syscall.SIGTERM):
logger.Printf("Got signal %q; stopping all tasks.", s)
for _, t := range GetTasks() {
t.Stop()
}
logger.Printf("Tasks all stopped after %s; quitting.", s)
os.Exit(0)
case os.Signal(syscall.SIGCHLD):
// Ignore.
default:
logger.Printf("unhandled signal: %T %#v", s, s)
}
}
}
示例11: handleSignals
func handleSignals() {
signals := make(chan os.Signal, 1)
signal.Notify(signals)
for s := range signals {
switch s {
case os.Interrupt, os.Signal(syscall.SIGTERM):
logger.Printf("%q: stop everything", s)
haproxy.Kill()
ctemplate.Kill()
os.Exit(0)
case os.Signal(syscall.SIGHUP):
fmt.Println("SIGHUP!")
haproxy.Restart()
case os.Signal(syscall.SIGCHLD):
// Ignore.
default:
logger.Printf("WTF %T %#v", s, s)
}
}
}
示例12: daemon
func daemon(config *docker.DaemonConfig) error {
if err := createPidFile(config.Pidfile); err != nil {
log.Fatal(err)
}
defer removePidFile(config.Pidfile)
server, err := docker.NewServer(config)
if err != nil {
return err
}
defer server.Close()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
go func() {
sig := <-c
log.Printf("Received signal '%v', exiting\n", sig)
server.Close()
removePidFile(config.Pidfile)
os.Exit(0)
}()
chErrors := make(chan error, len(config.ProtoAddresses))
for _, protoAddr := range config.ProtoAddresses {
protoAddrParts := strings.SplitN(protoAddr, "://", 2)
if protoAddrParts[0] == "unix" {
syscall.Unlink(protoAddrParts[1])
} else if protoAddrParts[0] == "tcp" {
if !strings.HasPrefix(protoAddrParts[1], "127.0.0.1") {
log.Println("/!\\ DON'T BIND ON ANOTHER IP ADDRESS THAN 127.0.0.1 IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
}
} else {
server.Close()
removePidFile(config.Pidfile)
log.Fatal("Invalid protocol format.")
}
go func() {
chErrors <- docker.ListenAndServe(protoAddrParts[0], protoAddrParts[1], server, true)
}()
}
for i := 0; i < len(config.ProtoAddresses); i += 1 {
err := <-chErrors
if err != nil {
return err
}
}
return nil
}
示例13: daemon
func daemon(pidfile string, protoAddrs []string, autoRestart, enableCors bool, flDns string) error {
if err := createPidFile(pidfile); err != nil {
log.Fatal(err)
}
defer removePidFile(pidfile)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
go func() {
sig := <-c
log.Printf("Received signal '%v', exiting\n", sig)
removePidFile(pidfile)
os.Exit(0)
}()
var dns []string
if flDns != "" {
dns = []string{flDns}
}
server, err := docker.NewServer(autoRestart, enableCors, dns)
if err != nil {
return err
}
chErrors := make(chan error, len(protoAddrs))
for _, protoAddr := range protoAddrs {
protoAddrParts := strings.SplitN(protoAddr, "://", 2)
if protoAddrParts[0] == "unix" {
syscall.Unlink(protoAddrParts[1])
} else if protoAddrParts[0] == "tcp" {
if !strings.HasPrefix(protoAddrParts[1], "127.0.0.1") {
log.Println("/!\\ DON'T BIND ON ANOTHER IP ADDRESS THAN 127.0.0.1 IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
}
} else {
log.Fatal("Invalid protocol format.")
os.Exit(-1)
}
go func() {
chErrors <- docker.ListenAndServe(protoAddrParts[0], protoAddrParts[1], server, true)
}()
}
for i := 0; i < len(protoAddrs); i += 1 {
err := <-chErrors
if err != nil {
return err
}
}
return nil
}
示例14: Daemon
// Daemon runs the remote api server `srv` as a daemon,
// Only one api server can run at the same time - this is enforced by a pidfile.
// The signals SIGINT, SIGKILL and SIGTERM are intercepted for cleanup.
func (srv *Server) Daemon() error {
if err := utils.CreatePidFile(srv.runtime.config.Pidfile); err != nil {
log.Fatal(err)
}
defer utils.RemovePidFile(srv.runtime.config.Pidfile)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
go func() {
sig := <-c
log.Printf("Received signal '%v', exiting\n", sig)
utils.RemovePidFile(srv.runtime.config.Pidfile)
srv.Close()
os.Exit(0)
}()
protoAddrs := srv.runtime.config.ProtoAddresses
chErrors := make(chan error, len(protoAddrs))
for _, protoAddr := range protoAddrs {
protoAddrParts := strings.SplitN(protoAddr, "://", 2)
switch protoAddrParts[0] {
case "unix":
if err := syscall.Unlink(protoAddrParts[1]); err != nil && !os.IsNotExist(err) {
log.Fatal(err)
}
case "tcp":
if !strings.HasPrefix(protoAddrParts[1], "127.0.0.1") {
log.Println("/!\\ DON'T BIND ON ANOTHER IP ADDRESS THAN 127.0.0.1 IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
}
default:
return fmt.Errorf("Invalid protocol format.")
}
go func() {
chErrors <- ListenAndServe(protoAddrParts[0], protoAddrParts[1], srv, true)
}()
}
for i := 0; i < len(protoAddrs); i += 1 {
err := <-chErrors
if err != nil {
return err
}
}
return nil
}
示例15: isProcessAlive
func isProcessAlive(p *os.Process) error {
err := p.Signal(os.Signal(syscall.Signal(0)))
if err == nil {
return nil
}
errno, ok := err.(syscall.Errno)
if !ok {
return ErrDeadOwner
}
switch errno {
case syscall.ESRCH:
return ErrDeadOwner
case syscall.EPERM:
return nil
default:
return err
}
}