mitranim / gow

Missing watch mode for Go commands. Watch Go files and execute a command like "go run" or "go test"
The Unlicense
751 stars 29 forks source link

Cannot run gow processes in shell background #20

Closed sgielen closed 2 years ago

sgielen commented 2 years ago

Hi! I'm trying to run multiple gow processes in background from a shell script. While testing, I noticed that running gow in a shell background doesn't work:

src$ gow run ./devproxy & 
[1] 80958
[1]  + 80958 suspended (tty output)  gow run ./devproxy                                                                                                                                  
src$ fg
[1]  + 80958 continued  gow run ./devproxy
[gow] interrupted system call

However, without backgrounding the process runs fine:

src$ gow run ./devproxy
2021/10/27 15:53:20 Ready for connections on port 8060.

I tried to figure out why; the first thing I tried was redirecting stdout and stderr to no avail:

$ gow run ./devproxy >foo.txt 2>&1 &
[1] 81130
[1]  + 81130 suspended (tty output)  gow run ./devproxy > foo.txt 2>&1                                                                                                                   
$ fg
[1]  + 81130 continued  gow run ./devproxy > foo.txt 2>&1
$ cat foo.txt
[gow] interrupted system call

Then, I tried to redirect stdin, but got a different error:

$ gow run ./devproxy </dev/null     
[gow] operation not supported by device

Same with /dev/zero.

Is it impossible to run gow in the background due to how it is written, or is this a bug that could be addressed?

sgielen commented 2 years ago

Looks like the following patch, and remapping stdin to /dev/null, serves as a workaround:

diff --git a/gow.go b/gow.go
index ba4b853..b1d04f5 100644
--- a/gow.go
+++ b/gow.go
@@ -166,7 +166,7 @@ func main() {
                if errors.Is(err, syscall.ENOTTY) {
                        log.Println("failed to set raw terminal mode: no terminal exists")
                } else {
-                       critical(err)
+                       log.Printf("failed to set raw terminal mode: %+v", err)
                }
        } else {
                termios = &state
src$ gow run ./devproxy </dev/null &
[1] 81690
[gow] failed to set raw terminal mode: operation not supported by device                                                                                                                 
src$ 2021/10/27 16:03:03 Ready for connections on port 8060.
src$ fg
mitranim commented 2 years ago

Just pushed a change that allows to avoid using raw mode:

gow -r=false

When false, gow will not attempt to use raw mode, so you won't even see a warning. Let me know if you run into any issues.

mitranim commented 2 years ago

Since there's a working solution, I'll go ahead and close this. Let me know if anything else is required.

sgielen commented 2 years ago

Thank you @mitranim, adding -r=false with the latest version indeed solves the issue :)