本文整理汇总了Golang中syscall.Getpgid函数的典型用法代码示例。如果您正苦于以下问题:Golang Getpgid函数的具体用法?Golang Getpgid怎么用?Golang Getpgid使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Getpgid函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Kill
func (cmd *Cmd) Kill() error {
switch cmd.State {
case Running:
cmd.State = Terminated
lg.Debugf("Cmd:\tKilling %v\n", cmd.JobID)
pgid, err := syscall.Getpgid(cmd.Cmd.Process.Pid)
if err != nil {
// Fall-back on error. Kill the main process only.
cmd.Cmd.Process.Kill()
break
}
// Kill the whole process group.
syscall.Kill(-pgid, 15)
case Finished:
lg.Debug("Cmd:\tKilling pgroup %v\n", cmd.JobID)
pgid, err := syscall.Getpgid(cmd.Cmd.Process.Pid)
if err != nil {
break
}
// Make sure to kill the whole process group,
// so there are no subprocesses left.
syscall.Kill(-pgid, 15)
case Initialized:
// This one is tricky, as the cmd's Start() might have
// been called and is already in progress, but the cmd's
// state is not Running yet.
usCallingStartOnce := false
cmd.StartOnce.Do(func() {
cmd.WaitOnce.Do(func() {
cmd.State = Invalidated
cmd.StatusCode = -2
cmd.Err = errors.New("invalidated")
lg.Debugf("Cmd: Invalidating %v\n", cmd.JobID)
close(cmd.Finished)
})
close(cmd.Started)
usCallingStartOnce = true
})
if !usCallingStartOnce {
// It was cmd.Start() that called StartOnce.Do(), not us,
// thus we need to wait for Started and try to Kill again:
<-cmd.Started
cmd.Kill()
}
}
return cmd.Err
}
示例2: Start
func (p *execProcess) Start() error {
if p.cmd.Stdout == nil {
p.cmd.Stdout = p.stdoutWriter
}
if p.cmd.Stderr == nil {
p.cmd.Stderr = p.stderrWriter
}
cmdString := strings.Join(p.cmd.Args, " ")
p.logger.Debug(execProcessLogTag, "Running command '%s'", cmdString)
if !p.keepAttached {
p.cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}
err := p.cmd.Start()
if err != nil {
return bosherr.WrapErrorf(err, "Starting command '%s'", cmdString)
}
if !p.keepAttached {
p.pgid = p.cmd.Process.Pid
} else {
p.pgid, err = syscall.Getpgid(p.pid)
if err != nil {
p.logger.Error(execProcessLogTag, "Failed to retrieve pgid for command '%s'", cmdString)
p.pgid = -1
}
}
return nil
}
示例3: Kill
func (r *Runner) Kill() error {
pgid, err := syscall.Getpgid(r.cmd.Process.Pid)
if err == nil {
syscall.Kill(-pgid, syscall.SIGTERM)
}
return err
}
示例4: Getpgid
func (k *PosixKernel) Getpgid(pid int) uint64 {
n, err := syscall.Getpgid(pid)
if err != nil {
return Errno(err)
}
return uint64(n)
}
示例5: killGroupProcess
//KillGroupProcess ...
func killGroupProcess(cmd *exec.Cmd) {
pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err == nil {
err = syscall.Kill(-pgid, 15)
printError("syscall Kill :", err)
sendMessage("info", "Run aborted\n")
}
}
示例6: Exec
// Satisfies the Listener interface and calls the relevant binary file
func (this *Cli) Exec(evt event.Event) {
this.Log.Handler(this, &evt)
bin := magicString(this.Config.GetBin(), evt)
stdin := magicString(this.Config.GetStdin(), evt)
args := append([]string(nil), this.Config.GetArgs()...)
for i, arg := range this.Config.GetArgs() {
args[i] = magicString(arg, evt)
}
cmd := exec.Command(bin, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Stdin = strings.NewReader(stdin)
var out bytes.Buffer
var sErr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &sErr
this.Log.Info("CLI CMD %s %s \"%s %s\"", evt.GetId(), this.GetName(), bin, strings.Join(args, " "))
err := cmd.Start()
if err != nil {
this.Log.Error("CLI ERR %s %s \"%s\"", evt.GetId(), this.GetName(), sErr.String())
this.Log.HandlerError(this, err.Error(), &evt)
return
}
timeOut := this.Config.GetTimeout()
if timeOut == 0 {
if err := cmd.Wait(); err != nil {
this.Log.Error("CLI ERROR %s", err.Error())
}
} else {
// this allows us to set a timeout on the cli (phantomjs def needs this)
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(time.Duration(timeOut) * time.Second):
pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err == nil {
syscall.Kill(-pgid, 15) // note the minus sign
this.Log.Warning("CLITIMEOUT %s %s", evt.GetId(), this.GetName())
}
<-done // allow goroutine to exit
case err := <-done:
if err != nil {
this.Log.Error("CLI ERROR %s", err.Error())
}
}
}
this.Log.Debug("CLI OUT %s %s \"%s\"", evt.GetId(), this.GetName(), out.String())
}
示例7: Info
func (c *command) Info() (pid, pgrp int) {
pid = c.proc.Process.Pid
pgrp, err := syscall.Getpgid(pid)
if err != nil {
c.test.Fatal(err)
}
return
}
示例8: killall
func killall(multierr io.Writer, cmd *exec.Cmd) {
cmd.Start()
pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err == nil {
syscall.Kill(-pgid, 15) // note the minus sign
cmd.Wait()
} else {
fmt.Fprintf(multierr, "\n%v\n", err)
}
}
示例9: sendSignalToProcess
func sendSignalToProcess(process *os.Process, what values.Signal, tryGroup bool) error {
if tryGroup {
pgid, err := syscall.Getpgid(process.Pid)
if err == nil {
if syscall.Kill(-pgid, syscall.Signal(what)) == nil {
return nil
}
}
}
process.Signal(syscall.Signal(what))
return nil
}
示例10: kill
// Kill a script run and all child processes
func (self *ScriptRun) kill() error {
if self.Cmd == nil {
return errors.New("Cmd is nil")
} else if self.Cmd.Process == nil {
return errors.New("Cmd.Process is nil")
}
pgid, err := syscall.Getpgid(self.Cmd.Process.Pid)
if err != nil {
return err
}
return syscall.Kill(-pgid, syscall.SIGKILL)
}
示例11: killProcessOnTimeout
// kills a cmd process based on config timeout settings
func killProcessOnTimeout(cmd *exec.Cmd, timeout time.Duration) (exitCode int, err error) {
// 1 deep channel for done
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
processGroupID, err := syscall.Getpgid(cmd.Process.Pid)
if err != nil {
return 0, err
}
grimProcessGroupID, err := syscall.Getpgid(os.Getpid())
if err != nil {
return 0, err
}
select {
case <-time.After(timeout):
exitCode = -23
err = errTimeout
case err := <-done:
if err != nil {
exitCode, err = getExitCode(err)
if err != nil {
return 0, fmt.Errorf("Build Error: %v", err)
}
}
}
if grimProcessGroupID != processGroupID {
syscall.Kill(-processGroupID, syscall.SIGKILL)
}
return
}
示例12: TestIPERF
func TestIPERF(t *testing.T) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
if <-c != syscall.SIGTERM {
panic(nil)
}
}()
if len(os.Getenv("IPERF")) != 0 {
cmd0 := exec.Command("iperf3", "-s", "", "-p", "5203")
cmd0.Start()
pid := cmd0.Process.Pid
go cmd0.Wait()
t0 := Accelerate("tcp://:41501-41504,udp://:42401-42404", "tcp://127.0.0.1:5203", BACKEND)
t0.WaitforAlive()
t1 := Accelerate("tcp://:50500", "tcp://127.0.0.1:41501-41504,udp://127.0.0.1:42401-42404", FRONTEND)
t1.WaitforAlive()
//iperfExec(exec.Command("iperf3", "-c", "127.0.0.1", "-p", "50500", "-R", "-P", "3"))
//iperfExec(exec.Command("iperf3", "-c", "127.0.0.1", "-p", "50500", "-b", "10M"))
cmd1 := exec.Command("iperf3", "-c", "127.0.0.1", "-p", "50500", "--get-server-output")
iperfExec(cmd1)
if len(os.Getenv("CPU")) != 0 {
f, err := os.Create("cpu.profile")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
cmd1.Wait()
cmd2 := exec.Command("iperf3", "-c", "127.0.0.1", "-p", "50500", "-R", "--get-server-output")
iperfExec(cmd2)
cmd2.Wait()
t0.Status()
t1.Status()
pgid, err := syscall.Getpgid(pid)
if err == nil {
syscall.Kill(-pgid, 15) // note the minus sign
}
}
}
示例13: killall
func killall(multierr io.Writer, cmd *exec.Cmd) {
pgid, err := syscall.Getpgid(cmd.Process.Pid)
// don't kill myself if cmd is running in same process group
if pgid == os.Getpid() || pgid == os.Getgid() {
cmd.Process.Kill()
return
}
if err == nil {
syscall.Kill(-pgid, 15) // note the minus sign
cmd.Wait()
} else {
fmt.Fprintf(multierr, "\n%v\n", err)
}
}
示例14: KillProcesses
func (x *ProcessManager) KillProcesses() error {
x.processes.Lock()
defer x.processes.Unlock()
for _, pi := range x.processes.values {
// we could potentially fail before the processes are correctly started
// in that case, do nothing
if pi.cmd.Process != nil {
pgid, err := syscall.Getpgid(pi.cmd.Process.Pid)
if err != nil {
return err
}
syscall.Kill(-pgid, 15)
}
}
return nil
}
示例15: cmdKill
func (i *instance) cmdKill(req *Request) (*Response, error) {
glog.Infoln("Server going down with SIGKILL")
glog.Flush()
pgid, err := syscall.Getpgid(0)
if err != nil {
glog.Errorln("Failed to get pgid", err)
os.Exit(1)
}
err = syscall.Kill(-pgid, syscall.SIGKILL)
if err != nil {
glog.Errorln("Failed to kill our process group", err)
os.Exit(1)
}
// We'll never get here...
return nil, nil
}