英文:
How to start a Go program as a daemon in Ubuntu?
问题
在Ubuntu中,启动Go程序作为守护进程的正确方法是什么?然后我将使用Monit来监控它。我应该像这样做吗:
go run myapp.go &;
有没有Go特定的事情我应该考虑?
英文:
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 &
Are there things specific to Go that I should take into account ?
答案1
得分: 45
你应该为你的程序构建一个可执行文件(go build
),然后要么为其编写一个 upstart 脚本,它将作为守护进程运行你的程序,要么使用像 daemonize 这样的外部工具。我更喜欢后者的解决方案,因为它不依赖于系统相关的 upstart。使用 daemonize,你可以像这样启动你的应用程序:
daemonize -p /var/run/myapp.pid -l /var/lock/subsys/myapp -u nobody /path/to/myapp.exe
这将为你提供一个行为良好的 Unix 守护进程,daemonize 会完成所有必要的守护进程准备工作。
英文:
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.
答案2
得分: 10
有一个关于在Go程序中守护进程的错误报告:http://code.google.com/p/go/issues/detail?id=227
但是,如果你只是想从进程中分离出来,我看到有以下建议:
nohup go run myapp.go &
或者
go run myapp.go & disown
你还可以使用进程管理器,比如编写一个init.d
,Startup
,或者使用像Supervisor这样的工具,我个人非常喜欢。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论