Can I daemonize a golang process without external tools?

huangapple go评论85阅读模式
英文:

Can I daemonize a golang process without external tools?

问题

我看到了很多关于如何在Go中将应用程序变为守护进程的建议,包括使用upstart或init.d。但是,为什么不自己来做呢?这些步骤似乎很简单:

  • 分叉并退出父进程
  • 更改文件模式掩码
  • 创建唯一的会话ID
  • 更改当前工作目录
  • 关闭文件描述符

(来源:1

那么,为什么不在你的程序中自己完成这些步骤,而要依赖第三方软件来完成这个简单的任务呢?

英文:

I've seen many different suggestions for how to daemonize an application written in Go, including using upstart or init.d. Why not just do it yourself though? It seems like the steps are simple enough:

  • Fork and exit parent
  • Change file mode mask
  • Create a unique session id
  • Change current working directory
  • Close file descriptors

(source)

So why not just do this in your program, rather than depending on third party software to do a simple task for you?

答案1

得分: 1

直接在Go程序中使用fork()是危险的。基于等效的C程序的典型守护进程实现是不安全的。

您可能对以下软件包使用的方法感兴趣:
https://github.com/VividCortex/godaemon

英文:

Messing directly with fork() from a Go program is perilous. A typical deamonize implementation based on an equivalent C program is not safe.

You may be interested by the approach used by the following package:
https://github.com/VividCortex/godaemon

huangapple
  • 本文由 发表于 2015年1月5日 08:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/27772097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定