Change the ruby process name in top

后端 未结 7 834
渐次进展
渐次进展 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

    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
    

提交回复
热议问题