Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ps时后台启动的程序,其命令行显式始终有-d=false #4

Open
sleepreading opened this issue Jan 2, 2020 · 1 comment
Open

Comments

@sleepreading
Copy link

源代码中有一段关键逻辑是 遇到-d=true的参数就强制替换为-d=false(假设该步骤为A), 然后exec.Command执行
经过测试, 步骤A非常关键, 缺少时会有问题.
但步骤A非常神奇, 希望能解释下其具体的作用.
因为ps查看进程时,用本库后台启动的进程始终会带有命令行参数-d=false,这非常具有误导性!

@icattlecoder
Copy link
Owner

!!这个daemon不是正确的实现方式,只是一个非常trick的一种做法,不应该在production中使用。

但步骤A非常神奇, 希望能解释下其具体的作用.

if *godaemon {
		args := os.Args[1:]
		i := 0
		for ; i < len(args); i++ {
			if args[i] == "-d=true" {
				args[i] = "-d=false"
				break
			}
		}
		cmd := exec.Command(os.Args[0], args...)
		cmd.Start()
		fmt.Println("[PID]", cmd.Process.Pid)
		os.Exit(0)
	}

程序启动时检查到 -d=true,则会以-d=false去启动新的进程,父进程退出,所以新的子进程 -d=false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants