Create a gist now

Instantly share code, notes, and snippets.

Update a process name in linux to change how it shows up in top and lsof
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment