How to change the process name of your Ruby script as shown by `top` and `ps`

It's super handy to be able to refer to processes by name. But default process names can be pretty cryptic. This post will show you how to set friendly process names, and even how to use the process name to provide status summaries for long running processes like Unicorn.

Whenever you run a program on linux or OSX, it runs inside of a process. And every process has a name. The name is what you see when you run a command like ps or top, or htop.

htop shows the process names in the rightmost column. htop shows the process names in the rightmost column.

Default Process Names Can Suck

By default a process's name is derived from the executable file name containing the program you're running. This works out nicely for most executables. After all, it makes sense that when you run "less", its process name should be "less."

But the default process names are less helpful when you have a Ruby script that you're running from the command line. In the example below, I'm running a ruby script that sleeps for five seconds. If I simultaneously run "ps" in another terminal window, I see that the process name for my sleeper is "ruby sleep_5_seconds.rb". If I were to add command line arguments, those would show up in the process name as well. This would make it difficult to refer to the process by name.

The full ruby command is listed as the process name The full ruby command is listed as the process name

How to Change the Process Name

Fortunately you can change the name of the current process easily with Ruby. Here's our updated script. It now sets its process name to "sleeper."

# `Process.setproctitle()` is in Ruby >= 2.1
# For earlier versions of Ruby, you can use
#   $PROGRAM_NAME = "sleeper"
# or 
#   $0 = "sleeper"

Process.setproctitle("sleeper")
sleep 5

Now, when we run the program and use ps to display its title, we get "sleeper"

Changing the process title in Ruby changes the output of `ps` and `top` Changing the process title in Ruby changes the output of ps and top

But even better, we can now easily refer to the process by name. Suppose I get tired of waiting for my sleeper to sleep. I can kill it using the command killall sleeper   .

You can use the `killall` command to terminate processes by name You can use the killall command to terminate processes by name

Displaying Server Status via Process Name

One interesting use of our new ability to change process titles is to display status information for long running processes. If you've ever run Unicorn, this should look familiar:

\-+= 27185 deply unicorn master -c simple_unicorn_config.rb -l0.0.0.0:8080
   |--- 27210 deply unicorn worker[0] -c simple_unicorn_config.rb -l0.0.0.0:8080
   |--- 27211 deply unicorn worker[1] -c simple_unicorn_config.rb -l0.0.0.0:8080
   |--- 27212 deply unicorn worker[2] -c simple_unicorn_config.rb -l0.0.0.0:8080
   |--- 27213 deply unicorn worker[3] -c simple_unicorn_config.rb -l0.0.0.0:8080

It's an instance of Unicorn with four child processes. Just as with our sleep_5_seconds.rb example, the process names just show the commands used to launch the processes.

It might be useful for the status lines to display if the worker is busy or idle. Something like this:

\-+= 27185 deply unicorn master -c simple_unicorn_config.rb -l0.0.0.0:8080
   |--- 27210 deply unicorn worker[0] -c simple_unicorn_config.rb -l0.0.0.0:8080 BUSY
   |--- 27211 deply unicorn worker[1] -c simple_unicorn_config.rb -l0.0.0.0:8080
   |--- 27212 deply unicorn worker[2] -c simple_unicorn_config.rb -l0.0.0.0:8080
   |--- 27213 deply unicorn worker[3] -c simple_unicorn_config.rb -l0.0.0.0:8080 BUSY

You could actually do this pretty easily with a Rack middleware. Here's an example of what that might look like:

class UpdateProcessTitle
  def initialize(app)
    @app = app
  end

  def call(env)
    title = $0
    $0 = $0 + " BUSY"
    status, headers, body = @app.call(env)
    $0 = title
    [status, headers, body]
  end
end

I have no idea of the performance implications of setting the process title on every web page request. So take this all with a grain of salt. But still, it's a pretty cool idea.

If you'd like to see a more advanced implementation of this idea - one that's actually been used in production - check out Thomas Varaneckas' great blog post on Overriding Unicorn Worker Process Names.

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Starr Horne

    Starr Horne is a Rubyist and Chief JavaScripter at Honeybadger.io. When she's not neck-deep in other people's bugs, she enjoys making furniture with traditional hand-tools, reading history and brewing beer in her garage in Seattle.

    More articles by Starr Horne
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like:

    “Everyone is in love with Honeybadger ... the UI is spot on.”
    Molly Struve, Sr. Site Reliability Engineer, Netflix
    Start free trial