<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/running-code-when-the-ruby-on-rails-webserver-boots-tvayfsf9x" -->

---
title: Running code when the Ruby on Rails webserver boots
description: Several approaches exist for running code exclusively when a Rails webserver boots, rather than during rake tasks, console sessions, or other Rails commands....
canonical: https://daily.dev/posts/running-code-when-the-ruby-on-rails-webserver-boots-tvayfsf9x
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Running code when the Ruby on Rails webserver boots | daily.dev
og:description: Several approaches exist for running code exclusively when a Rails webserver boots, rather than during rake tasks, console sessions, or other Rails commands....
og:url: https://daily.dev/posts/running-code-when-the-ruby-on-rails-webserver-boots-tvayfsf9x
og:image: https://api.daily.dev/og/posts/tvAyFSF9X.png
og:image:alt: Running code when the Ruby on Rails webserver boots
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Running code when the Ruby on Rails webserver boots

**[RUBYLAND](https://daily.dev/sources/rubyla)** · 3 min read · 1 upvotes · 0 comments

## Summary

Several approaches exist for running code exclusively when a Rails webserver boots, rather than during rake tasks, console sessions, or other Rails commands. Options include: using the Railtie `server do` block (Rails 6.1+, requires `Rails.application.load_server` in config.ru), placing code directly in config.ru, writing a Puma plugin for deeper lifecycle hooks, inspecting the call stack for webserver fingerprints (brittle but broadly compatible), or using Rack middleware to trigger code on the first request. The best choice depends on context — gems/engines should prioritize broad compatibility, while application-specific code should avoid leaving hidden footguns.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://island94.org/2026/07/running-code-when-the-ruby-on-rails-webserver-boots>

## Questions this post answers

### How do I run code only when Puma boots a Rails app, not during rake tasks or the console?

Use the Railtie#server hook added in Rails 6.1, invoked via a server do block in application.rb or a gem's engine.rb, which fires when config.ru is evaluated. Alternatively, place code directly in config.ru, write a Puma plugin, or use Rack middleware or a controller before_action to run logic on the first request.

_Compare these Rails webserver boot hook patterns and community discussion threads on daily.dev._

### Why isn't my Rails server hook code running even though I defined it with config.server do?

The server hook only fires if config.ru actually calls Rails.application.load_server, and this line is missing from many Rails projects even though the default generator template includes it. Double-check that your config.ru still contains that call, since it can be silently dropped or never added after rails new was run.

_Track Rails config.ru gotchas like this one alongside other framework quirks on daily.dev._

### How does GoodJob detect whether Rails is running inside a webserver versus a console or rake task?

GoodJob's in_webserver? method checks Rails.const_defined?(:Server) and, if that's not set, greps the current callstack for fingerprints like config.ru, puma/request, rack/handler, or jruby/rack/rails_booter for JRuby. This zero-configuration approach maximizes compatibility across old Rails versions and webservers but is considered brittle compared to newer hooks like Puma plugins or Railtie#server.

_See how other gem authors solve webserver-detection tradeoffs like this on daily.dev._

## Similar posts on daily.dev

- [Middleware in Rails - FastRuby.io](https://daily.dev/posts/middleware-in-rails---fastruby-io-cgmltefoz) · RUBYLAND · 0 upvotes · 0 comments
- [Get in, human: cut Rails boot time with require-profiler and this guide—Martian Chronicles, Evil Martians’ team blog](https://daily.dev/posts/get-in-human-cut-rails-boot-time-with-require-profiler-and-this-guide-martian-chronicles-evil-mar-xwe166tlh) · Martian Chronicles · 0 upvotes · 0 comments
- [Build Once, Launch Ten Times: The Rails Engine SaaS Strategy](https://daily.dev/posts/build-once-launch-ten-times-the-rails-engine-saas-strategy-tj0fdf3up) · Ruby Flow · 0 upvotes · 0 comments
- [Warming up the Puma master before it forks](https://daily.dev/posts/warming-up-the-puma-master-before-it-forks-ubpjz1eku) · 37signals Dev · 2 upvotes · 0 comments

---

Tags: [#ruby](https://daily.dev/tags/ruby), [#rails](https://daily.dev/tags/rails)

[View this post on daily.dev](https://daily.dev/posts/running-code-when-the-ruby-on-rails-webserver-boots-tvayfsf9x)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Running code when the Ruby on Rails webserver boots","url":"https://daily.dev/posts/running-code-when-the-ruby-on-rails-webserver-boots-tvayfsf9x","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/running-code-when-the-ruby-on-rails-webserver-boots-tvayfsf9x"},"datePublished":"2026-07-31T21:54:23.152Z","dateModified":"2026-09-14T07:29:15.572Z","description":"Several approaches exist for running code exclusively when a Rails webserver boots, rather than during rake tasks, console sessions, or other Rails commands....","image":"https://media.daily.dev/image/upload/s--58gMhC4P--/f_auto/v1722860399/public/Placeholder%2012","thumbnailUrl":"https://media.daily.dev/image/upload/s--58gMhC4P--/f_auto/v1722860399/public/Placeholder%2012","isAccessibleForFree":true,"articleSection":"RUBYLAND","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"RUBYLAND","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/rubyla","url":"https://daily.dev/sources/rubyla"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/running-code-when-the-ruby-on-rails-webserver-boots-tvayfsf9x","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"ruby,rails","timeRequired":"PT3M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"RUBYLAND","item":"https://daily.dev/sources/rubyla"},{"@type":"ListItem","position":3,"name":"Running code when the Ruby on Rails webserver boots"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/running-code-when-the-ruby-on-rails-webserver-boots-tvayfsf9x#faq","mainEntity":[{"@type":"Question","name":"How do I run code only when Puma boots a Rails app, not during rake tasks or the console?","acceptedAnswer":{"@type":"Answer","text":"Use the Railtie#server hook added in Rails 6.1, invoked via a server do block in application.rb or a gem's engine.rb, which fires when config.ru is evaluated. Alternatively, place code directly in config.ru, write a Puma plugin, or use Rack middleware or a controller before_action to run logic on the first request. Compare these Rails webserver boot hook patterns and community discussion threads on daily.dev."}},{"@type":"Question","name":"Why isn't my Rails server hook code running even though I defined it with config.server do?","acceptedAnswer":{"@type":"Answer","text":"The server hook only fires if config.ru actually calls Rails.application.load_server, and this line is missing from many Rails projects even though the default generator template includes it. Double-check that your config.ru still contains that call, since it can be silently dropped or never added after rails new was run. Track Rails config.ru gotchas like this one alongside other framework quirks on daily.dev."}},{"@type":"Question","name":"How does GoodJob detect whether Rails is running inside a webserver versus a console or rake task?","acceptedAnswer":{"@type":"Answer","text":"GoodJob's in_webserver? method checks Rails.const_defined?(:Server) and, if that's not set, greps the current callstack for fingerprints like config.ru, puma/request, rack/handler, or jruby/rack/rails_booter for JRuby. This zero-configuration approach maximizes compatibility across old Rails versions and webservers but is considered brittle compared to newer hooks like Puma plugins or Railtie#server. See how other gem authors solve webserver-detection tradeoffs like this on daily.dev."}}]}
```

