// Package procprio starts helper processes with reduced CPU and IO priority. package procprio import "os/exec" // lookPath is replaceable in tests. var lookPath = exec.LookPath // Command returns the executable and arguments to run binary with args. // // With low set, the process is started through ionice (idle IO class) and // nice (level 19), so it only receives CPU time and disk bandwidth that no // other process on the server needs. Both tools exec the target, so the PID, // process group, signals and exit status stay those of binary. A tool that // is not installed is skipped; without either tool the command is unchanged. func Command(low bool, binary string, args []string) (string, []string) { if !low { return binary, args } var prefix []string if path, err := lookPath("ionice"); err == nil { prefix = append(prefix, path, "-c", "3") } if path, err := lookPath("nice"); err == nil { prefix = append(prefix, path, "-n", "19") } if len(prefix) == 0 { return binary, args } full := make([]string, 0, len(prefix)+len(args)) full = append(full, prefix[1:]...) full = append(full, binary) full = append(full, args...) return prefix[0], full }