Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore SIGINT while running child process #58

Merged
merged 2 commits into from
Oct 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Ignore SIGINT while running child process
  • Loading branch information
CGamesPlay committed Jul 18, 2022
commit a9f5d779b0a86d06a55b3448a055499cef3658d0
16 changes: 14 additions & 2 deletions task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
Expand Down Expand Up @@ -89,6 +90,17 @@ func (r *Runnable) Run(lookupPath string, args []string, env []string) error {
return fmt.Errorf("nothing to run (add script, command, or exec key)")
}

func (r *Runnable) runInternal(cmd *exec.Cmd) error {
if err := cmd.Start(); err != nil {
return err
}
// Ignore SIGINT while waiting for a child process. The child process will
// receive the signal and abort normally, then we will continue.
signal.Ignore(os.Interrupt);
defer signal.Reset(os.Interrupt);
return cmd.Wait();
}

// RunScript runs the target shell `script` file.
func (r *Runnable) RunScript(lookupPath string, args []string, env []string) error {
var path = r.Script
Expand All @@ -114,7 +126,7 @@ func (r *Runnable) RunScript(lookupPath string, args []string, env []string) err
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
return r.runInternal(cmd)
}

// RunCommand runs the `command` via the shell.
Expand All @@ -125,7 +137,7 @@ func (r *Runnable) RunCommand(args []string, env []string) error {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
return r.runInternal(cmd)
}

// RunExec runs the `exec` command.
Expand Down