Adding Rails application to /usr/local/etc/rc.d/

I was adding recently Rails application to FreeBSD startup scripts, to have it running after power-on. I have chosen /usr/local/etc/rc.d/ as place to add such script. I’ve copied some existing script (I believe it was from Apache) and customized all required variables.

But after reboot Rails application was not running. Starting script as a root from CLI was successful:


[bsd60 ~]# /usr/local/etc/rc.d/railsapp.sh start
Starting railsapp.
=> Booting WEBrick...
=> UID have been changed to 1001
=> Rails application started on http://0.0.0.0:3001
[2006-08-22 16:31:14] INFO WEBrick 1.3.1
[2006-08-22 16:31:14] INFO ruby 1.8.4 (2005-12-24) [i386-freebsd6]

NOTE: It was not running as a root ;-)) http://dev.rubyonrails.org/ticket/5788

What was wrong? During startup I’ve noticed following message on console:

env: ruby: No such file or directory

It was the reason. During startup I’ve changed current dir to Rails app root and running script/server command. It uses env command to execute ruby:

[bsd60 ~]$ tail /PRODUCTION/railsapp/script/server
#!/usr/bin/env ruby
[bsd60 ~]$

On FreeBSD boxes default ruby location is /usr/local/bin and this directory is not on search list during startup. So I thought about following solutions:

  • Change #! line in server to point direct to ruby interpreter, not using env command. But this would required change after every deployment (application is written with portability on mind, so I don’t want change RailTies without good reason), so I discarded that idea
  • Next I wanted to call ruby directly from startup script – using command="/usr/local/bin/ruby /PATH/TO/RAILS/script/server" (command it is shell variable from FreeBSD startup scripts). But then restarting server results in following output:


/usr/local/etc/rc.d/railsapp.sh: WARNING: /PRODUCTION/railsapp/script/server !=ELF
/etc/rc.subr: line 608: [: /usr/local/bin/ruby: binary operator expected
Starting railsapp.
=> Booting WEBrick...
=> UID have been changed to 1001
=> Rails application started on http://0.0.0.0:3001
[2006-08-22 16:44:07] INFO  WEBrick 1.3.1
[2006-08-22 16:44:07] INFO  ruby 1.8.4 (2005-12-24) [i386-freebsd6]

This strange error probably is caused by checks in FreeBSD startup scripts which expect command to be binary, rather than script. And space in command confuses them, so they check second part of string. But never mind. So now it is needed to change command and command_args. command should be just /usr/local/bin/ruby and command_args ruby script location and other options. In my case it was:


command_args=" /PRODUCTION/railsapp/script/server -p 3001 -e production -d -u 1001"

Or after all this I thought about simplest solution. Just add /usr/local/bin to PATH variable on the beginning of startup script (PATH="$PATH:/usr/local/bin")

Join the Conversation

2 Comments

  1. WOOT!!! You totally just solved several hours of headscratching. With my level of *nix knowledge this was starting to get frustrating.

    I was also getting the “env: ruby” line in the console log. Changing the shebang line to reference the actual location of ruby didn’t work and actually gave me some fatal ruby errors. I was starting to think it didn’t know where to find ruby (hence the error, of course).

    Didn’t know you could mod the PATH variable in the .sh script. That fixed it all. Now my mongrel clusters are starting on reboot.

    THANK YOU!!!

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.