记录进程输出

发布于 2024-12-08 19:12:23 字数 318 浏览 0 评论 0原文

我使用方法system来启动一个进程。该进程的 pid 存储在文件 worker.pid 中,

但是我需要生成该进程的日志,如何存储该进程的输出?

正在使用以下命令创建进程:

system "bundle exec rake resque:work >> ./resque.log QUEUE=* PIDFILE=#{pid_file} &"

PS:我使用的是 ruby​​ 1.8,BACKGROUND=yes 不起作用。 PS2:Linux平台

I used the method system to start a process. the pid of that process is being stored in a file worker.pid

however I need to generate the log of this process, how can I store the output of this process?

the process is being created with this command:

system "bundle exec rake resque:work >> ./resque.log QUEUE=* PIDFILE=#{pid_file} &"

P.S.: I am using ruby 1.8, BACKGROUND=yes won`t work.
P.S.2: platform linux

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

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

发布评论

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

评论(2

谁的年少不轻狂 2024-12-15 19:12:23

也许您正在寻找的是 IO.popen

这可以让你分叉一个子进程并通过 IO 对象访问它的输出

# fork off a one-off task
# and return the output as a string
ls = IO.popen("ls")
ls.read

# or return an array of lines
IO.popen("ls").readlines

# or create a continuing task
tail = IO.popen("tail -f /some/log/file.log")
loop do
  puts tail.gets
end

我建议你阅读文档,
但您也可以写入流,并做各种聪明的事情。

Maybe what you're looking for is IO.popen

This lets you fork off a subprocess and access it's output via an IO object

# fork off a one-off task
# and return the output as a string
ls = IO.popen("ls")
ls.read

# or return an array of lines
IO.popen("ls").readlines

# or create a continuing task
tail = IO.popen("tail -f /some/log/file.log")
loop do
  puts tail.gets
end

I suggest you read the documentation,
but you can also write to the stream, and do all sorts of clever stuff.

笑着哭最痛 2024-12-15 19:12:23

如果我理解您想要正确实现的目标,那么您正在寻找 Open3 课程。 http://www.ruby-doc.org /stdlib-1.8.7/libdoc/open3/rdoc/Open3.html

If I'm understanding what you are trying to achieve correctly, you are looking for the Open3 class. http://www.ruby-doc.org/stdlib-1.8.7/libdoc/open3/rdoc/Open3.html

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