go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/system/exec2/exec2.go (about) 1 // Copyright 2019 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package exec2 16 17 import ( 18 "context" 19 "os/exec" 20 ) 21 22 // Cmd is like exec.Cmd, but supports terminate and process containment. 23 type Cmd struct { 24 *exec.Cmd 25 26 attr attr 27 } 28 29 // CommandContext is like exec.CommandContext, but it uses process group by 30 // default and supports timeout in Wait function. 31 func CommandContext(ctx context.Context, name string, arg ...string) *Cmd { 32 cmd := &Cmd{ 33 Cmd: exec.CommandContext(ctx, name, arg...), 34 } 35 36 cmd.setupCmd() 37 38 return cmd 39 } 40 41 // Start starts command with appropriate setup. 42 func (c *Cmd) Start() error { 43 return c.start() 44 } 45 46 // Wait waits to process to finish. 47 func (c *Cmd) Wait() error { 48 return c.wait() 49 } 50 51 // Terminate sends SIGTERM on unix or CTRL+BREAK on windows. 52 func (c *Cmd) Terminate() error { 53 return c.terminate() 54 } 55 56 // Kill kills process. 57 func (c *Cmd) Kill() error { 58 return c.kill() 59 }