Skip to content

Instantly share code, notes, and snippets.

@eric
Created November 9, 2011 07:39
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eric/1350729 to your computer and use it in GitHub Desktop.
Save eric/1350729 to your computer and use it in GitHub Desktop.
Update a process name in linux to change how it shows up in top and lsof
#
# Eric Lindvall <eric@5stops.com>
#
# Update the process name for the process you're running in.
#
# This will allow top, lsof, and killall to see the process as the
# name you specify.
#
# Just use:
#
# LinuxProcName.set_proc_name('deeplinux')
#
# To change the way the process name looks in ps, use:
#
# $0 = "deeplinux"
#
require 'ffi'
module LinuxProcName
# Set process name
PR_SET_NAME = 15
module LibC
extend FFI::Library
ffi_lib FFI::Library::LIBC
begin
attach_function :prctl, [ :ulong, :ulong, :ulong, :ulong ], :int
rescue FFI::NotFoundError
# We couldn't find the method
end
end
def self.set_proc_name(name)
return false unless LibC.respond_to?(:prctl)
# 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