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"
)
Leave a Reply