Ubuntu Pastebin

Paste from Chipaca at Fri, 7 Oct 2016 10:22:15 +0000

Download as text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main

import (
	"fmt"
	"os"
	"os/signal"
	"time"
)

func reraise() {
	ch := make(chan os.Signal, 1)
	signal.Notify(ch, os.Interrupt)
	s := <-ch
	fmt.Println("reraising signal", s)
	signal.Stop(ch)
	p, err := os.FindProcess(os.Getpid())
	if err != nil {
		// WUT
		panic(err)
	}
	p.Signal(os.Interrupt)
}

func catch() {
	ch := make(chan os.Signal, 1)
	signal.Notify(ch, os.Interrupt)
	s := <-ch
	fmt.Println("exiting on signal", s)
	os.Exit(1)
}

func usage() {
	fmt.Printf("usage: %s (nothing|ignore|catch|reraise)\n", os.Args[0])
	os.Exit(1)
}

func main() {
	if len(os.Args) < 2 {
		usage()
	}

	switch os.Args[1] {
	case "nothing":
		fmt.Println("doing nothing (script should stop)")
	case "ignore":
		fmt.Println("ignoring ^C (script should continue)")
		signal.Ignore(os.Interrupt)
	case "catch":
		fmt.Println("handling, not reraising (script should continue)")
		go catch()
	case "reraise":
		fmt.Println("handling & reraising (script should stop)")
		go reraise()
	default:
		usage()
	}

	time.Sleep(10 * time.Second)
	fmt.Println("done")
}
Download as text