Heim > Fragen und Antworten > Hauptteil
巴扎黑2017-04-17 16:30:44
重新翻了我的旧代码,然后再拿出来测试了可以!
package main
import (
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
)
func docmd(Num int) {
time.Sleep(time.Second * 3)
cmd := exec.Command("hostname")
buf, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("%d Result: %s", Num, buf)
}
func main() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
for {
select {
case <-signalChan:
log.Println("正在结束!")
docmd(10)
os.Exit(0)
}
}
}()
docmd(1)
fmt.Println("How much can I fuck you?")
docmd(2)
time.Sleep(time.Second * 10)
}