I write Go program that will run another Go program that will daemonize.
I am wondering how much time the first program must wait before its child process is daemonising.
cmd := exec.Command(path1)
cmd.Start()
// exit here
or
cmd := exec.Command(path1)
cmd.Run()
// exit here
or
cmd := exec.Command(path1)
cmd.Start()
time.Sleep(5 * time.Second)
// exit here
If I use cmd.Run()
what command/action in started daemon program will end "waiting" in first program.
Daemonizing a process is just a fancy way of forking the process. That means that the process you start will exit as soon as the daemonized process is started. Therefore you want to use Run
, which will wait for the started process to return (the successful fork).
Process A:
|
|`-- run(B)
| Process B:
| |
| |`-- daemonize(C)
| |
| `-- exit
|
`-- daemonizing done
If you want to wait for a state of the daemon, the most reliable way is to be signalled by the daemon. For example using a socket, a file or shared memory.