Skip to content

Commit 44f52eb

Browse files
authored
Allow package users to set the child process priority (#17)
1 parent f67973e commit 44f52eb

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

localexec.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99

1010
// LocalExecer executes command on the local system.
1111
type LocalExecer struct {
12+
// ChildProcessPriority overrides the default niceness of all child processes launch by LocalExecer.
13+
ChildProcessPriority *int
1214
}
1315

1416
func (l *localProcess) Stdin() io.WriteCloser {

localexec_unix.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ func (l LocalExecer) Start(ctx context.Context, c Command) (Process, error) {
9191
if err != nil {
9292
return nil, xerrors.Errorf("start command: %w", err)
9393
}
94+
95+
if l.ChildProcessPriority != nil {
96+
pid := process.cmd.Process.Pid
97+
niceness := *l.ChildProcessPriority
98+
err := syscall.Setpriority(syscall.PRIO_PROCESS, pid, niceness)
99+
if err != nil {
100+
return nil, xerrors.Errorf("set process (pid: %d) priority to (niceness: %d): %w", pid, niceness, err)
101+
}
102+
}
94103
}
95104

96105
return &process, nil

0 commit comments

Comments
 (0)