运行一个与发起者分离的可执行文件。

huangapple go评论87阅读模式
英文:

Run an executable file detached from originator

问题

我正在尝试弄清楚如何使用ForkExec从Go中运行可执行文件。到目前为止,我尝试了以下代码:

var sysProcAttr = syscall.SysProcAttr{}
sysProcAttr.Setsid = true
sysProcAttr.Foreground = false
var procAttr = syscall.ProcAttr{
    Dir: "/",
    Env: nil,
    Sys: &sysProcAttr,
}

processID, err = syscall.ForkExec("/home/ubuntu/.dotnet/dotnet", []string{"/home/ubuntu/my-executable-net-dll-file"}, &procAttr)
return processID, err

这会创建一个进程,但当我关闭Go应用程序时,这些进程也会关闭。问题是 - 如何将进程与应用程序分离?似乎Setsid不起作用,剩下的选项只能使用nohup或"bash -c",但我担心结果会相同。

英文:

I'm trying to figure out how to run an executable file from Go using ForkExec. Having tried this so far:

var sysProcAttr = syscall.SysProcAttr{}
sysProcAttr.Setsid = true
sysProcAttr.Foreground = false
var procAttr = syscall.ProcAttr{
	Dir: "/",
	Env: nil,
	Sys: &sysProcAttr,
}

processID, err = syscall.ForkExec("/home/ubuntu/.dotnet/dotnet", []string{"/home/ubuntu/my-executable-net-dll-file"}, &procAttr)
return processID, err

This creates a process, but when I close the Go application these processes close as well. The question is - how do I detach the process from it? It appears that Setsid doesn't work, the only options left are to use nohup or "bash -c", but I fear the results will be the same.

答案1

得分: 1

当我关闭Go应用程序时,这些进程也会关闭。

os/execCommand似乎没有相同的行为,至少不是固有的:

% pid=$(go run t.go); while ps $pid; do sleep 1; done
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
[2021/11/08 08:23:45 CST ] ~
package main

import (
	"os/exec"
	"fmt"
)

func main() {
	c := exec.Command("bash", "-c", "sleep 3; echo slept 3; sleep 3; echo slept 3 more")
	if err := c.Start(); err != nil {
		panic(err)
	}
	fmt.Println(c.Process.Pid)
}

这可能取决于生成的进程行为。请参阅https://unix.stackexchange.com/questions/158727/is-there-any-unix-variant-on-which-a-child-process-dies-with-its-parent

英文:

> when I close the Go application these processes close as well.

os/exec's Command doesn't seem to have the same behavior, at least not inherently:

% pid=$(go run t.go); while ps $pid; do sleep 1; done
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
33132 s006  S      0:00.00 bash -c sleep 3; echo slept 3; sleep 3; echo slept 3 more
  PID   TT  STAT      TIME COMMAND
[2021/11/08 08:23:45 CST ] ~
package main

import (
	"os/exec"
	"fmt"
)

func main() {
	c := exec.Command("bash", "-c", "sleep 3; echo slept 3; sleep 3; echo slept 3 more")
	if err := c.Start(); err != nil {
		panic(err)
	}
	fmt.Println(c.Process.Pid)
}

It could depend on spawned process behavior. See https://unix.stackexchange.com/questions/158727/is-there-any-unix-variant-on-which-a-child-process-dies-with-its-parent

huangapple
  • 本文由 发表于 2021年11月8日 21:11:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/69884049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定