更改顶部的 ruby​​ 进程名称

发布于 2024-07-12 10:32:41 字数 139 浏览 7 评论 0原文

我想更改 linux/unix top 命令中显示的 ruby​​ 进程的名称。 我已经尝试过该

$0='miname'

方法,但它仅适用于 ps 命令,并且在顶部,进程不断显示为“ruby”

I would like to change the name of the ruby process that gets displayed in the linux/unix top command. I have tried the

$0='miname'

approach but it only works with the ps command and in top the process keeps getting displayed as "ruby"

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

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

发布评论

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

评论(7

原来是傀儡 2024-07-19 10:32:41

Dave Thomas 发表了一篇有趣的帖子 在 Rails 中执行此操作。 实际的进程名称更改代码没有任何特定于 Rails 的内容。 他使用 $0='name' 方法。 当我按照他的步骤操作时,pstop 中的名称发生了更改。

在帖子中,如果您的 top 版本默认不显示命令的简短版本,他建议使用 c 键盘命令。

Dave Thomas had an interesting post on doing this in rails. There's nothing rails specific about the actual process name change code. He uses the $0='name' approach. When I followed his steps the name was changed in ps and top.

In the post he suggests using the c keyboard command if your version of top doesn't show the short version of the command by default.

渡你暖光 2024-07-19 10:32:41

Ruby 2.1 引入了 Process.setproctitle 用于此目的的方法:

Process.setproctitle("My new title")

Ruby 2.1 introduced a Process.setproctitle method for this purpose:

Process.setproctitle("My new title")
猛虎独行 2024-07-19 10:32:41

我不认为 Ruby 具有内置功能 (setproctitle(3))。 您可能应该尝试查看 ruby-ffi 并创建 setproctitle(3 )

编辑:我知道你有答案,但我想向你展示一些使用 ffi 的代码:

require "ffi"
#
module LibC
  extend FFI::Library

  attach_function :setproctitle, [:string, :varargs], :void
end

LibC.setproctitle("Ruby: executing %s", :string, $0)

在 OS X 上不起作用,因为 setproctitle(3) 不存在,在 FreeBSD 上工作。

I don't think Ruby has the facility builtin (setproctitle(3)). You should probably try to look at ruby-ffi and create the interface to setproctitle(3).

EDIT: I know you have your answer but I want to show you some code to use ffi:

require "ffi"
#
module LibC
  extend FFI::Library

  attach_function :setproctitle, [:string, :varargs], :void
end

LibC.setproctitle("Ruby: executing %s", :string, $0)

Does not work on OS X because setproctitle(3) does not exist, works on FreeBSD.

以可爱出名 2024-07-19 10:32:41

$0 = 'Foo' 方法可以工作——但是许多版本的 top 会要求你用 'c' 打开命令行模式。 我们在这里使用 Rails 和 CentOS 来实现这一方法。 工作是一种享受

The $0 = 'Foo' method works -- but many versions of top will require you to toggle command-line mode on with 'c'. We this very method here with rails and CentOS. Works a treat

违心° 2024-07-19 10:32:41

我遇到了类似的问题,通过将 Dave Thomas 帖子中的技术放入机架中间件中,而不是之前/之后的模式中,对其进行了一些更新。

将其放入 lib/rack/set_process_title.rb 中:

# Set the process title to the URI being processed 
#- useful for debugging slow requests or those that get stuck
class Rack::SetProcessTitle
  def initialize(app)
    @app = app
  end
  def call(env)
    $0 = env['REQUEST_URI'][0..80]

    @status, @headers, @response = @app.call(env)

    $0 = env['REQUEST_URI'][0..80] + '*'

    [@status, @headers, @response]
  end
end

...这位于 config/environment.rb 的末尾:

Rails.configuration.middleware.insert_after Rack::Lock, Rack::SetProcessTitle

博客文章中的更多文字: http://blog.actbluetech.com/2011/06/set-your-process-name-in -top-and-ps.html

I had a similar problem, updated the technique from the Dave Thomas post a little by putting it in a rack middleware, rather than the before/after pattern.

Put this in lib/rack/set_process_title.rb:

# Set the process title to the URI being processed 
#- useful for debugging slow requests or those that get stuck
class Rack::SetProcessTitle
  def initialize(app)
    @app = app
  end
  def call(env)
    $0 = env['REQUEST_URI'][0..80]

    @status, @headers, @response = @app.call(env)

    $0 = env['REQUEST_URI'][0..80] + '*'

    [@status, @headers, @response]
  end
end

... and this goes at the end of config/environment.rb:

Rails.configuration.middleware.insert_after Rack::Lock, Rack::SetProcessTitle

More words in the blog post: http://blog.actbluetech.com/2011/06/set-your-process-name-in-top-and-ps.html

尘曦 2024-07-19 10:32:41

我知道 Keltia 已经发布了非常类似的内容,但 Linux 没有 setproctitle(3)。
Linux 从 2.6.9 版本开始就在 prctl() 中提供了此功能。
我使用了 Fiddle/DL,因为它们默认包含在 Ruby 中。

require("fiddle")

def set_process_name_linux(name)
    Fiddle::Function.new(
        Fiddle::Handle["prctl".freeze], [
            Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP,
            Fiddle::TYPE_LONG, Fiddle::TYPE_LONG,
            Fiddle::TYPE_LONG
        ], Fiddle::TYPE_INT
    ).call(15, name, 0, 0, 0)
end

def set_process_name_unknown(name)
    warn("No implementation for this OS.".freeze)
end

def set_process_name(name)
    case RUBY_PLATFORM.split("-".freeze)[1]
    when "linux".freeze
        set_process_name_linux(name)
    else
        set_process_name_unknown(name)
    end
end

I know Keltia already posted something very similar, but Linux doesn't have setproctitle(3).
Linux has had this functionality in prctl() since version 2.6.9.
I used Fiddle/DL since they are included by default with Ruby.

require("fiddle")

def set_process_name_linux(name)
    Fiddle::Function.new(
        Fiddle::Handle["prctl".freeze], [
            Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP,
            Fiddle::TYPE_LONG, Fiddle::TYPE_LONG,
            Fiddle::TYPE_LONG
        ], Fiddle::TYPE_INT
    ).call(15, name, 0, 0, 0)
end

def set_process_name_unknown(name)
    warn("No implementation for this OS.".freeze)
end

def set_process_name(name)
    case RUBY_PLATFORM.split("-".freeze)[1]
    when "linux".freeze
        set_process_name_linux(name)
    else
        set_process_name_unknown(name)
    end
end
倾其所爱 2024-07-19 10:32:41

根据 @jessehz 的回答,以下代码在我的 Linux X86_64 上完美运行。
已测试 Ruby 1.9.3、2.0、2.1、2.2、2.3。

  1. 它将更改 ps top 命令的输出。
  2. 它可以是kill 或用pkill、pgrep、killall 发出信号。

完美的!

def set_process_name_linux(name)
  handle = defined?(DL::Handle) ? DL::Handle : Fiddle::Handle

  Fiddle::Function.new(
    handle['prctl'.freeze], [
      Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP,
      Fiddle::TYPE_LONG, Fiddle::TYPE_LONG,
      Fiddle::TYPE_LONG
    ], Fiddle::TYPE_INT
  ).call(15, name, 0, 0, 0)
  $PROGRAM_NAME = name
end
set_process_name_linux('dummy')

From @jessehz answer, following code work perfect on my linux X86_64.
Ruby 1.9.3, 2.0, 2.1, 2.2, 2.3 is tested.

  1. It will change the output in ps top command.
  2. It can be kill or signal with pkill, pgrep, killall.

Perfect!

def set_process_name_linux(name)
  handle = defined?(DL::Handle) ? DL::Handle : Fiddle::Handle

  Fiddle::Function.new(
    handle['prctl'.freeze], [
      Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP,
      Fiddle::TYPE_LONG, Fiddle::TYPE_LONG,
      Fiddle::TYPE_LONG
    ], Fiddle::TYPE_INT
  ).call(15, name, 0, 0, 0)
  $PROGRAM_NAME = name
end
set_process_name_linux('dummy')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文