5

When developing a Rails 4.1.6 project, I can start a Webrick server with:

rails s

Before the server has processed any requests, I can:

  • Stop it with control-C
  • Suspend it with control-Z

After the server has processed a request, I can still stop it with control-C, but I can no longer suspend it with control-Z. Typing control-Z echoes "^Z" to the terminal, but the server continues to run and will processes any requests it receives.

Why does control-Z fail to suspend the server once the server has processed a request?

Details

Starting the server:

$ rails s
Warning: NLS_LANG is not set. fallback to US7ASCII.
=> Booting WEBrick
=> Rails 4.1.6 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
[2014-10-28 15:16:09] INFO  WEBrick 1.3.1
[2014-10-28 15:16:09] INFO  ruby 2.1.2 (2014-05-08) [i686-linux]
[2014-10-28 15:16:09] INFO  WEBrick::HTTPServer#start: pid=29538 port=3000

Control-Z before the server has processed any requests:

$ rails s
Warning: NLS_LANG is not set. fallback to US7ASCII.
=> Booting WEBrick
=> Rails 4.1.6 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
^Z
[1]+  Stopped                 rails s

Processing a request:

Started GET "/" for 127.0.0.1 at 2014-10-28 15:18:24 -0700
  ActiveRecord::SchemaMigration Load (4.2ms)  SELECT "SCHEMA_MIGRATIONS".* FROM "SCHEMA_MIGRATIONS"
   (69.1ms)  SELECT column_name AS name, data_type AS sql_type, data_default, nullable, virtual_column, hidden_column, data_type_owner AS sql_type_owner, DECODE(data_type, 'NUMBER', data_precision, 'FLOAT', data_precision, 'VARCHAR2', DECODE(char_used, 'C', char_length, data_length), 'RAW', DECODE(char_used, 'C', char_length, data_length), 'CHAR', DECODE(char_used, 'C', char_length, data_length), NULL) AS limit, DECODE(data_type, 'NUMBER', data_scale, NULL) AS scale FROM all_tab_cols WHERE owner = 'DOCUMENT_DIRECTOR_DEVELOPMENT' AND table_name = 'SCHEMA_MIGRATIONS' AND hidden_column = 'NO' ORDER BY column_id
Processing by IndexController#index as JSON
  Rendered index/index.json.jbuilder (3.1ms)
Completed 200 OK in 10ms (Views: 9.9ms | ActiveRecord: 0.0ms)

Control-Z after processing a request, pressed many times because I mean business:

^Z^Z^Z^Z^Z^Z

Gemfile:

source 'https://rubygems.org'
source 'http://gems:9292'

gem 'activerecord-oracle_enhanced-adapter',
  git: 'https://github.com/wconrad/oracle-enhanced.git',
  branch: 'better-system-password-entry'
gem 'apipie-rails'
gem 'capistrano-rails', group: :development
gem 'capistrano-rvm'
gem 'cucumber-rails', :require => false, group: [:test]
gem 'cute_print'
gem 'database_cleaner', group: [:development, :test]
gem 'factory_girl_rails', group: [:development, :test]
gem 'jbuilder'
gem 'jsonpath', group: :test
gem 'maruku'
gem 'newrelic_rpm'
gem 'opacs_billing'
gem 'opacs_db'
gem 'rails'
gem 'rails-erd', group: :development
gem 'retryable'
gem 'rspec-rails', group: [:development, :test]
gem 'ruby-oci8'
gem 'sass-rails'
gem 'sdoc', group: :doc
gem 'simplecov', require: false, group: :test
gem 'spring', group: :development
gem 'versionist'
gem 'yard'

Versions:

  • MRI 2.1.2
  • Rails 4.1.6
  • bash 4.2.37
  • Debian GNU/Linux "wheezy"
Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
  • All you want is Control-Z to suspend the process while it's done processing a request? – Surya Nov 02 '14 at 14:32
  • @User089247 That's right. The use case is: control-Z, then "bg" to resume the server in the background, then tail a log. – Wayne Conrad Nov 02 '14 at 14:40
  • Why not running the process as daemon itself then? `rails s -d` and then `tail -f log/development.log` – Surya Nov 02 '14 at 14:48
  • @User089247 Because the ease of "rails s" and then having the rails log on the console without a separate command. – Wayne Conrad Nov 02 '14 at 15:09
  • 1
    Wayne, no `: 1 dup ; DAY + swap; DOLLAR + ;`? – Cary Swoveland Dec 11 '14 at 20:04
  • @CarySwoveland Nice. I have worked in stack based languages, but never to the point of becoming competent. Also, I want to be sure: You are commenting on my profile, not on this question, right? – Wayne Conrad Dec 11 '14 at 20:11
  • Yes, your profile. Do you recall 'Lotus 1-2-3', and one of its clones, 'VP Planner'? A friend wrote the latter in FORTH. I was enamoured with the language for awhile. – Cary Swoveland Dec 11 '14 at 20:38

1 Answers1

2

In one of the comments you wrote in your question's comment section, you say:

The use case is: control-Z, then "bg" to resume the server in the background, then tail a log.

I think the issue is in putting the server in the background instead of in the foreground (using command fg). If you put the server in background, the console won't be able to stop it anymore until you put it in the foreground, so CTRL-Z won't work. So, I would try the sequence below:

  1. rails s
  2. CONTROL-Z (program is paused)
  3. fg (program is resumed)
  4. CONTROL-Z (does the program stop again?)

If you want to keep putting your server in the background, then you cand send the STOP signal to it, not by typing CONTROL-Z, but by sending the signal using kill:

kill -STOP <SERVER_PID> 

or

kill -s STOP <SERVER_PID>

(depends on your system).

To resume the process:

kill -CONT <SERVER_PID>

or

kill -s CONT <SERVER_PID>
Claudi
  • 5,224
  • 17
  • 30
  • I appreciate your attention to the question. Thanks! The rails server does not typically (ever?) read from the console, so backgrounding it will never cause it to pause when waiting for input. Step 2 of the sequence above is what is failing. Using "kill -s STOP..." does indeed work. – Wayne Conrad Nov 05 '14 at 12:19
  • I used bad words... When I said "reading from console" I was referring to the ability of the console to send the STOP signal when CONTROL-Z is hit. In other words, CONTROL-Z tells the console to send the STOP signal to the FOREGROUND process (background processes are not affected, and it should be not!). I've just edited the answer. – Claudi Nov 05 '14 at 12:27
  • The trouble is that control-Z, bg, fg, all work normally _until the server processes a request_. After the server processes a request, it is as though a control-Z at the console does not send a STOP signal to the server. – Wayne Conrad Nov 05 '14 at 13:38
  • 1
    OK, I understand. So even if Webrick has never been paused before, if it receives a request, it can't process it. – Claudi Nov 05 '14 at 14:03