How to start a Go program as a daemon in Ubuntu?

后端 未结 2 1533
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 08:49

What is the proper way to start a Go program as a daemon in Ubuntu ? I will then monitor it with Monit. Should I just do something like:

go run myapp.go &         


        
相关标签:
2条回答
  • 2020-12-04 09:09

    You should build an executable for your program (go build) and then either write a script for upstart and it will run your program as a daemon for you, or use an external tool like daemonize. I prefer the latter solution, because it does not depend on a system-dependent upstart. With daemonize you can start your application like

    daemonize -p /var/run/myapp.pid -l /var/lock/subsys/myapp -u nobody /path/to/myapp.exe
    

    This will give you a well-behaving unix daemon process with all necessary daemon preparations done by daemonize.

    0 讨论(0)
  • 2020-12-04 09:18

    There is a bug report regarding the ability to daemonize from within a Go program: http://code.google.com/p/go/issues/detail?id=227

    But if what you are after is just detaching from the process I have seen recommendations to either do one of the following:

    nohup go run myapp.go
    

    or

    go run myapp.go & disown
    

    You can also make use of a process manager, like writing an init.d, Startup, or using something like Supervisor, which I personally really like.

    0 讨论(0)
提交回复
热议问题