tags:

views:

852

answers:

3

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 but it only works with the ps command and in top the process keeps getting displayed as "ruby"

Thanks for any hints in advance.

+3  A: 

I don't think Ruby has the facility builtin (setproctitle(3)). You should probably try to look at ruby-ffi and create the interface to setproctitle(3).

EDIT: I know you have your answer but I want to show you some code to use ffi:

require "ffi"
#
module LibC
  extend FFI::Library

  attach_function :setproctitle, [:string, :varargs], :void
end

LibC.setproctitle("Ruby: executing %s", :string, $0)

Does not work on OS X because setproctitle(3) does not exist, works on FreeBSD.

Keltia
I didn't know about this, Keltia. Very cool.
Gordon Wilson
+4  A: 

Dave Thomas had an interesting post on doing this in rails. There's nothing rails specific about the actual process name change code. He uses the $0='name' approach. When I followed his steps the name was changed in ps and top.

In the post he suggests using the c keyboard command if your version of top doesn't show the short version of the command by default.

Gordon Wilson
I have tried the c keyboard command and it works with Linux machines. The top command from Mac OS X doesn't seem to have an equivalent option.
muesan
+1  A: 

The $0 = 'Foo' method works -- but many versions of top will require you to toggle command-line mode on with 'c'. We this very method here with rails and CentOS. Works a treat

mikey