Ubuntu – Starting command on boot with correct user env vars

rubyrvmthinUbuntuupstart

I am attempting to create a start-up script that will run this command on boot for my ubuntu 12.04 server:

bundle exec thin -R config.ru start -p 3030

When I run it as a regular user it works just fine, the thin web server starts and I can access my page that I need. Below is the start up script I am trying to use /etc/init/thin.conf:

description "thin"
version "1.0"
author "Me"

env LANG=en_US.UTF-8
env APP_HOME=/home/username/onlinetraining_dashboard
respawn
start on runlevel [23]
script
cd $APP_HOME
sudo -u username /home/username/.rvm/gems/ruby-1.9.3-p448@global/bin/bundle exec thin -R config.ru start -p 3030 > $APP_HOME/log/thin.log 2>&1
end script

I will then type "sudo start thin" to run the script and it should start on boot.

When I start the service I get this error in the log file:

/usr/bin/env: ruby_executable_hooks: No such file or directory

I am using ruby with RVM 1.9.3.

Here is my .bash_profile that I am assuming lets me run the command bundle exec thin -R config.ru start -p 3030 without errors:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

I also tried this without success in the script.

sudo -u username bundle exec thin -R config.ru start -p 3030 > $APP_HOME/log/thin.log 2>&1

Thanks for the help.

Best Answer

you should use wrappers, you can read more about them here: http://rvm.io/integration/init-d