Change the ruby process name in top

后端 未结 7 836
渐次进展
渐次进展 2021-02-18 14:19

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

相关标签:
7条回答
  • 2021-02-18 14:57

    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')
    
    0 讨论(0)
提交回复
热议问题