NetManiac

Witold Rugowski on web20 wave with Ruby on Rails

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

Posted on August 22, 2006 - Filed Under FreeBSD, Ruby

If you're new here, you may want to subscribe to my RSS feed. You can also get updates by email Thanks for visiting!

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:


/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")

Popularity: 100% [?]

Hits for this post: 46509

Similar Posts

Comments

2 Responses to “Adding Rails application to /usr/local/etc/rc.d/”

  1. JJ on August 25th, 2006 16:55

    http://nhw.pl/wp/2006/08/22/adding-rails-application-to-usrlocaletcrcd/#more-80

    Would you mind posting your whole Ruby script starter that you used in /usr/local/etc/rc.d?
    I would greatly appreciate it!

    Regards,
    JJ

  2. matte on November 17th, 2006 0:32

    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 Reply