Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly start a process that will daemonize using Go?

Tags:

linux

go

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.

like image 397
Artem Avatar asked Oct 19 '25 13:10

Artem


1 Answers

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.

like image 85
nemo Avatar answered Oct 21 '25 02:10

nemo