Trap signal of terminal stop

383 views
Skip to first unread message

Archos

unread,
May 1, 2013, 8:09:48 PM5/1/13
to golan...@googlegroups.com
I'm trying to trap CTRL-Z, in Linux, to avoid that the process can run in background, but it does not works

// * * *
package main

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

func main() {
    sig := make(chan os.Signal, 1)
    signal.Notify(sig, syscall.SIGTSTP) // CTRL-Z

L:
    for {
        select {
        case <-sig:

        case <-time.After(10 * time.Second):
            fmt.Println("timed out")
            break L
        }
    }
    fmt.Println("exit")
}
// * * *

And according to the next documentation, SIGTSTP is the signal used at pressing CTRL-Z:
http://www2.chrishardick.com:1099/Notes/Computing/C/UnixSignals.html

Why is failling? Do I have used a worng signal to be caught?

minux

unread,
May 1, 2013, 8:20:18 PM5/1/13
to Archos, golan...@googlegroups.com
On Thu, May 2, 2013 at 4:09 AM, Archos <raul...@sent.com> wrote:
I'm trying to trap CTRL-Z, in Linux, to avoid that the process can run in background, but it does not works
what do you mean by "it does not work"?

your program works for me (after added a print after line "case <-sig:"), each time ^Z
is pressed, the message is printed and the program is not put into background.

andrey mirtchovski

unread,
May 1, 2013, 8:21:05 PM5/1/13
to Archos, golang-nuts
works for me on linux and osx.

$ ./t
^Z^Z^Z^Z^Z^Z^Z^Z
timed out
exit

Archos

unread,
May 1, 2013, 8:31:28 PM5/1/13
to golan...@googlegroups.com

El miércoles, 1 de mayo de 2013 21:20:18 UTC+1, minux escribió:

On Thu, May 2, 2013 at 4:09 AM, Archos <raul...@sent.com> wrote:
I'm trying to trap CTRL-Z, in Linux, to avoid that the process can run in background, but it does not works
what do you mean by "it does not work"?
After of pressing CTRL-Z, the program runs in background.

My system is Ubuntu 13.04, 64 bits
go version devel +3478ecc801f6 Wed May 01 10:00:05 2013 -0400 linux/amd64

Archos

unread,
May 1, 2013, 8:33:50 PM5/1/13
to golan...@googlegroups.com
 One moment, I was running it using `go run program.go`; that was the problem.

Ugorji Nwoke

unread,
May 1, 2013, 10:23:53 PM5/1/13
to golan...@googlegroups.com
I'm not sure of your usecase, but I had a situation where I thought it was not getting trapped but it was. In case this is helpful ...

If you pipe your program to anything (e.g. tee, etc), your process will not get the signal.

myapp | tee out.txt (never got signal)
myapp (got signal)
Reply all
Reply to author
Forward
0 new messages