在 Ruby 中创建一个带有双叉的守护进程

发布于 2024-08-11 09:52:19 字数 111 浏览 8 评论 0原文

在 Ruby 中创建行为良好的 Unix 或 Linux 守护进程的正确方法是什么?

行为良好的守护进程的定义是什么?以及如何在 Ruby 中编写这样的程序?

What is the proper way to create a well-behaved Unix or Linux daemon in Ruby?

What is the definition of a well-behaved daemon anyway, and how would one write such a program in Ruby?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水染的天色ゝ 2024-08-18 09:52:19

根据 Stevens 的UNIX 环境中的高级编程第 13 章,这是制作一个行为良好的 Unix 守护进程的过程:

  1. 分叉并让父进程退出。这使得 shell 或引导脚本认为命令已完成。另外,保证子进程不是进程组组长(setsid 接下来的前提条件)
  2. 调用setsid 创建新会话。这做了三件事:
    1. 该进程成为新会话的会话领导者
    2. 该进程成为新进程组的进程组领导者
    3. 该进程没有控制终端
  3. 可以选择再次分叉并让父退出。这保证了守护进程不是会话领导者,也不能获取控制终端(在 SVR4 下)
  4. 将当前工作目录更改为 / 以避免干扰安装和卸载
  5. 将文件模式创建掩码设置为 000允许稍后创建具有任何所需权限的文件。
  6. 关闭从父级继承的不需要的文件描述符(无论如何都没有控制终端):stdout、stderr 和 stdin。

现在有一个文件可以跟踪 PID,Linux 发行版启动脚本大量使用该文件。一定要写出孙子的 PID,要么是第二个 fork 的返回值(第 3 步),要么是第 3 步之后 getpid() 的值。

这是一个 Ruby 实现,大部分翻译自书上的,但是使用了双叉并写出了守护进程 PID。

# Example double-forking Unix daemon initializer.

raise 'Must run as root' if Process.euid != 0

raise 'First fork failed' if (pid = fork) == -1
exit unless pid.nil?

Process.setsid
raise 'Second fork failed' if (pid = fork) == -1
exit unless pid.nil?
puts "Daemon pid: #{Process.pid}" # Or save it somewhere, etc.

Dir.chdir '/'
File.umask 0000

STDIN.reopen '/dev/null'
STDOUT.reopen '/dev/null', 'a'
STDERR.reopen STDOUT

According to Stevens's Advanced Programming in the UNIX Environment chapter 13, this is the procedure to make a well-behaved Unix daemon:

  1. Fork and have the parent exit. This makes the shell or boot script think the command is done. Also, the child process is guaranteed not to be a process group leader (a prerequisite for setsid next)
  2. Call setsid to create a new session. This does three things:
    1. The process becomes a session leader of a new session
    2. The process becomes the process group leader of a new process group
    3. The process has no controlling terminal
  3. Optionally fork again and have the parent exit. This guarantes that the daemon is not a session leader nor can it acquire a controlling terminal (under SVR4)
  4. Change the current working directory to / to avoid interfering with mounting and unmounting
  5. Set file mode creation mask to 000 to allow creation of files with any required permission later.
  6. Close unneeded file descriptors inherited from the parent (there is no controlling terminal anyway): stdout, stderr, and stdin.

Nowadays there is a file to track the PID which is used heavily by Linux distribution boot scripts. Be sure to write out the PID of the grandchild, either the return value of the second fork (step 3) or the value of getpid() after step 3.

Here is a Ruby implementation, mostly translated from the book, but with the double-fork and writing out the daemon PID.

# Example double-forking Unix daemon initializer.

raise 'Must run as root' if Process.euid != 0

raise 'First fork failed' if (pid = fork) == -1
exit unless pid.nil?

Process.setsid
raise 'Second fork failed' if (pid = fork) == -1
exit unless pid.nil?
puts "Daemon pid: #{Process.pid}" # Or save it somewhere, etc.

Dir.chdir '/'
File.umask 0000

STDIN.reopen '/dev/null'
STDOUT.reopen '/dev/null', 'a'
STDERR.reopen STDOUT
世态炎凉 2024-08-18 09:52:19

继杰森的精彩回应之后,我在这里编写了更完整的实现:

https://gist.github.com/1372491 /b76b60fb1842bf0507f47869ab19ad50a045b214

除了双分叉和将 pid 写入文件之外,我还实现了日志记录。

另一个有趣的实现是在 Unicorn 中:

https://github.com/ defunkt/unicorn/blob/master/lib/unicorn/launcher.rb

Following on from Jason's awesome response I have written a fuller implementation here:

https://gist.github.com/1372491/b76b60fb1842bf0507f47869ab19ad50a045b214

I have implemented logging in addition to the double fork and writing of the pid to file.

Another interesting implementation is in Unicorn:

https://github.com/defunkt/unicorn/blob/master/lib/unicorn/launcher.rb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文