public
Last active

Update a process name in linux to change how it shows up in top and lsof

  • Download Gist
linux_proc_title.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
require 'ffi'
 
module LinuxProcTitle
# Set process name
PR_SET_NAME = 15
 
module LibC
extend FFI::Library
ffi_lib FFI::Library::LIBC
 
attach_function :prctl, [ :ulong, :ulong, :ulong, :ulong ], :int
end
 
 
def self.set_proc_title(title)
set_proc_name(title[/^([^ ]+)/, 1])
$0 = title
end
 
def self.set_proc_name(name)
# The name can be up to 16 bytes long, and should be null-terminated if
# it contains fewer bytes.
name = name.slice(0, 16)
ptr = FFI::MemoryPointer.from_string(name)
LibC.prctl(PR_SET_NAME, ptr.address, 0, 0)
ensure
ptr.free if ptr
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.