I'm a new Linode/Linux user running Debian 6. I'm trying to get my Unicorn server to start at boot, but for some reason it is not, and I'm not able to track down any error message. Nginx is starting fine, and I have a multi-user RVM install. My gut feeling is that is has something to do with RVM. This is my unicorn_init.sh file in /rails/todo, and there's a symlink to it at /etc/init.d/unicorn:
# unicorn_init.sh
#!/bin/sh
set -e
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/rails/todo
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="$APP_ROOT/bin/unicorn_rails -D -c $APP_ROOT/config/unicorn.rb -E production"
GEM_HOME="/usr/local/rvm/gems/ruby-1.9.2-p290#global"
action="$1"
set -u
old_id="$PID.oldbin"
cd $APP_ROOT || exit 1
export GEM_HOME=$GEM_HOME
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $old_pid && kill -$1 `cat $old_pid`
}
case $action in
start)
sig 0 && echo >&2 "Already running" && exit 0
su -c "$CMD" - root
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
su -c "$CMD" - root
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $old_pid && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $old_pid
then
echo >&2 "$old_pid still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su -c "$CMD" - root
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
I'm 99% of the way to getting my setup working—any advice would be much appreciated.
Here is the output of $ update-rc.d unicorn defaults:
update-rc.d: using dependency based boot sequencing
insserv: warning: script 'unicorn' missing LSB tags and overrides
insserv: There is a loop between service nginx and unicorn if stopped
insserv: loop involving service unicorn at depth 2
insserv: loop involving service nginx at depth 1
insserv: Stopping unicorn depends on nginx and therefore on system facility `$all' which can not be true!
insserv: exiting now without changing boot order!
update-rc.d: error: insserv rejected the script header
update-rc.d: error: insserv rejected the script header
The start of your file looks like:
# unicorn_init.sh
#!/bin/sh
The shebang line (#!/bin/sh) MUST be the very first line of the file.
I can't comment on the loop-detection messages as I haven't ever seen 'em before. It's possible something you say in /etc/init.d/nginx is expressing dependency on unicorn, but I don't see anything in the unicorn init which expresses a dependency on nginx, so the loop isn't clear.
insserv: warning: script 'unicorn' missing LSB tags and overrides
You should add LSB info to your init script http://wiki.debian.org/LSBInitScripts
Related
I'm trying to set a rails test server, deployed using capistrano.
I know my capistrano scripts are working as it deploys to the production server using the same scripts without a problem.
During deployment, unicorn should be started, to do this
sudo service unicorn_appname start
is called.
This gives the following error:
Job for unicorn_appname.service failed because the control process exited with error code. See "systemctl status unicorn_appname.service" and "journalctl -xe" for details.
When I check sudo journalctl -u unicorn_appname is see
systemd[1]: Starting LSB: starts the unicorn web server...
su[3790]: Successful su for user by root
su[3790]: + ??? root:user
su[3790]: pam_unix(su:session): session opened for user user by (uid=0)
unicorn_appname[3787]: -su: bundle: command not found
systemd[1]: unicorn_appname.service: Control process exited, code=exited status=127
systemd[1]: Failed to start LSB: starts the unicorn web server.
systemd[1]: unicorn_appname.service: Unit entered failed state.
systemd[1]: unicorn_appname.service: Failed with result 'exit-code'.
/etc/init.d/unicorn_appname exists and when in /etc/init.d
./unicorn_appname start works
sudo ./unicorn_appname start however gives -su: bundle: command not found
however which bundle and sudo which bundle both show the same path (/home/user/.rbenv/shims/bundle)
If possible I don't want to change the scripts, as they work on the other server.
So I think there's some setting different or missing on the new server – but I have no more idea where to look.
This is the content of unicorn_appname:
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the unicorn web server
# Description: starts unicorn
### END INIT INFO
set -e
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/user/apps/appname/current
PID_DIR=$APP_ROOT/tmp/pids
PID=$PID_DIR/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c /home/user/apps/appname/shared/config/unicorn.rb -E production"
AS_USER=user
set -u
OLD_PIN="$PID.oldbin"
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}
workersig () {
workerpid="$APP_ROOT/tmp/pids/unicorn.$2.pid"
test -s "$workerpid" && kill -$1 `cat $workerpid`
}
run () {
if [ "$(id -un)" = "$AS_USER" ]; then
eval $1
else
su -c "$1" - $AS_USER
fi
}
case "$1" in
start)
sig 0 && echo >&2 "Already running" && exit 0
run "$CMD"
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
kill_worker)
workersig QUIT $2 && exit 0
echo >&2 "Worker not running"
;;
restart|reload)
sig USR2 && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $OLD_PIN && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $OLD_PIN
then
echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
run "$CMD"
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
Any more info you'll need?
EDIT:
user is the user I use to deploy. For root on this system, nothing is installed. Can that be the problem?
I think your problem (may) be:
cd $APP_ROOT; bundle exec unicorn -D -c /home/user/apps/appname/shared/config/unicorn.rb -E production"
Assuming that is the same command you're using on your test server, you'll need to change the environment to test.
I think I found the solution:
On the prod server I had a /etc/profile.d/rbenv.sh that's missing on the new server.
That's its content:
export RBENV_ROOT=/home/user/.rbenv
export PATH=$RBENV_ROOT/shims:$RBENV_ROOT/bin:$PATH
I'm trying to deploy a Ruby on Rails application using nginx and unicorn. When I try to run my unicorn init script, I get the following error:
$ sudo service rails_app start
Starting rails_app
-su: bundle: command not found
It fails to run on this command:
su - complab -c "bundle exec unicorn -c config/unicorn.rb -E production -D"
However, running unicorn manually while logged in as $USER works just fine:
$ bundle exec unicorn -c config/unicorn.rb -E production
I have verified that the path set on the init script is correct. On further investigation, I discovered that while $USER has ruby version 2.2.0 installed via rbenv, running su - $USER -c "ruby -v" shows that it's running on 1.9.3.
I have already run rbenv global 2.2.0 but still it doesn't work.
I thought su meant I was able to temporarily changing the user who owns the session? Why is it that su $USER shows a different ruby version from when I am actually logged in as $USER?
Hope you can help me out!
The reason is that sudo doesn't preserve environment variable. And since rbenv is using user environment it doesn't work out of the box. You can use a command like this to run ruby with sudo:
ruby=`which ruby` && sudo $ruby -v
If you want to run unicorn under the user you have installed rbenv into you have to change these to match your app:
!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts unicorn
# Description: starts uniconr using start-stop-daemon
### END INIT INFO
set -u
set -e
export PATH=/path/to/rbenv/bin:$PATH
export RBENV_DIR=/path/to/rbenv
export RBENV_ROOT=/path/to/rbenv
APP_ROOT=/path/to/rack_app/root
PID=$APP_ROOT/tmp/pids/unicorn.pid
RAILS_ENV=production
export PATH="$RBENV_ROOT/shims:$RBENV_ROOT/rbenv:$PATH"
CMD="bundle exec $RBENV_ROOT/shims/unicorn -D -E $RAILS_ENV -c config/unicorn/$RAILS_ENV.rb"
old_pid="$PID.oldbin"
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $old_pid && kill -$1 `cat $old_pid`
}
case $1 in
start)
sig 0 && echo >&2 "Already running" && exit 0
$CMD
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
$CMD
;;
upgrade)
sig USR2 && exit 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && 1
;;
*)
echo >&2 "Usage $0 <start|stop|restart|upgrade|rotate|force-stop>"
exit 1
;;
esac
How do I set up unicorn shell file to use ruby-2.1.2#my_gemset so that it can run as sudo service unicorn start. My Rails Application config folder contain unicorn.sh file which has the following content.
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Manage unicorn server
# Description: Start, stop, restart unicorn server for a specific application.
### END INIT INFO
set -e
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-90}
APP_ROOT=/home/user/Documents/workspace/MyApp
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT;bundle exec bin/unicorn -c $APP_ROOT/config/unicorn.rb -E development"
AS_USER="www-data"
action="$1"
set -u
OLD_BIN="$PID.oldbin"
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_BIN && kill -$1 `cat $OLD_BIN`
}
run () {
if [ "$(id -un)" = "$AS_USER" ]; then
eval $1
else
su -c "$1" - $AS_USER
fi
}
case "$1" in
start)
sig 0 && echo >&2 "Already running" && exit 0
run "$CMD"
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;
upgrade)
if sig USR2 && sleep 3
then
n=$TIMEOUT
while test -s $OLD_BIN && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $OLD_BIN
then
echo >&2 "$OLD_BIN still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
run "$CMD"
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
but when i run sudo service unicorn start I got this error
/var/lib/gems/1.8/gems/bundler-1.5.3/lib/bundler/runtime.rb:220: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777
Could not find rake-10.4.2 in any of the sources
Run `bundle install` to install missing gems.
But actually i am using ruby-2.1.2#my_gemset. How do I tell shell script to use this gemset using rvm.
My bin/unicorn file has this content
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'unicorn' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
require 'rubygems'
require 'bundler/setup'
load Gem.bin_path('unicorn', 'unicorn')
I know i have to change the path of /usr/bin/env ruby to my ruby but don't know how to do this. Also please let me know how can I set up unicorn shell script to use specific gemset with specific ruby.Thanks
I'm using rvm in my remote server to manage my gems. I've committed the .ruby-gemset, and .ruby-version so that it's uniform. In my deployment script, I added the snippet below to make sure I'm running the command in the right folder with the correct set of gems. So execute:
source ~/.rvm/scripts/rvm; cd #{current_path}; /etc/init.d/unicorn #{command}"
I'm pretty sure this can be refactored further but I haven't gotten around to updating my script for a while. But so far, it's been working. I'm using Capistrano 3.
I haven't done this before but i think this will work -
Add this line -
CMD_TO_USE_GEMSET="cd $APP_ROOT; rvm use ruby-2.1.2#my_gemset"
after
CMD="cd $APP_ROOT;bundle exec bin/unicorn -c $APP_ROOT/config/unicorn.rb -E development"
in your unicorn.sh.
And write this in "start" and "restart|reload" section of your unicorn.sh after run "$CMD" -
run "$CMD_TO_USE_GEMSET"
and now run the command to restart unicorn -
sudo service unicorn start
I have the following Chef routine to generate a script to start/stop a unicorn daemon.
However I always get a Permission denied when Capistrano execute this file. What I'm doing wrong with its permissions?
# recipes/default.rb
template "/etc/init.d/unicorn_#{node['pod']['app_name']}" do
owner node['pod']['user']
group node['pod']['user']
mode 777
source "unicorn_init.erb"
variables( :app_name => node['pod']['app_name'], :user => node['pod']['user'] )
end
# templates/default/unicorn_init.rb
#!/bin/sh
set -e
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/<%= #user %>/apps/<%= #app_name %>/current
PID=/var/run/unicorn/unicorn_<%= #app_name %>.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
AS_USER=<%= #user %>
set -u
OLD_PIN="$PID.oldbin"
sig () {
test -s "$PID" && su -c "kill -$1 `cat $PID`" - $AS_USER
}
oldsig () {
test -s $OLD_PIN && sudo -c "kill -$1 `cat $OLD_PIN`" - $AS_USER
}
run () {
if [ "$(id -un)" = "$AS_USER" ]; then
eval $1
else
su -c "$1" - $AS_USER
fi
}
case "$1" in
start)
sig 0 && echo >&2 "Already running" && exit 0
run "$CMD"
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $OLD_PIN && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $OLD_PIN
then
echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
run "$CMD"
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
As you can see, it doesn't set the correct mode for the file:
[2013-05-27T20:42:00+02:00] INFO: Processing template[/etc/init.d/unicorn_pod] action create (pod::default line 38)
[2013-05-27T20:42:00+02:00] INFO: template[/etc/init.d/unicorn_pod] owner changed to 1001
[2013-05-27T20:42:00+02:00] INFO: template[/etc/init.d/unicorn_pod] group changed to 110
[2013-05-27T20:42:00+02:00] INFO: template[/etc/init.d/unicorn_pod] mode changed to 1411
** [out :: localhost] -r----x--t 1 deployer deployer 1453 2013-05-27 17:48 unicorn_pod
See the docs. Mode should be octal or string:
template "/etc/init.d/unicorn_#{node['pod']['app_name']}" do
[...]
mode 0777 #or mode "777"
[...]
end
I have installed nginx with unicorn on an Ubuntu 12.04 server. Everything works, site, db, unicorn...good. So I'm trying to make sure that after a reboot, nginx and unicorn start up. I have set update-rc.d for my unicorn process, but it doesn't start/work after a reboot. I suspect it has something to do with ubuntu's use of "service" as opposed to "/etc/init.d/unicorn_init "
In other words:
If I execute:
$ /etc/init.d/unicorn_init start
unicorn starts up just fine, no errors.
If I execute:
$ service unicorn_init start
it fails and unicorn does not start.
I've think it has something to do with paths. Ive added environment PATHS to PATH, GEM_PATH, & GEM_HOME, but I still receive the same results
1, If I run /usr/local/rvm/gems/ruby-1.9.3-p194/bin/unicorn, I get errors:
usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find unicorn (>= 0) amongst[bigdecimal-1.1.0, io-console-0.3, json-1.5.4, minitest-2.5.1, rake-0.9.2.2, rdoc-3.9.4] (Gem::LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems.rb:1231:in `gem'
from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/unicorn:18:in `<main>'
2, If I run /var/rails/web-app/bin/unicorn, I get errors:
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- bundler/setup (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /var/rails/web-app/bin/unicorn:14:in `<main>'
Any help would be greatly appreciated! Thanks
you should use an unicorn wrapper script that will include all the required environment variables:
rvm wrapper 1.9.3 ruby-1.9.3 unicorn
It will generate ruby-1.9.3_unicorn use this instead of just unicorn in the init script.
You can find more details on wrappers with:
rvm wrapper
In case when work is done via bundler (like capitrano) then generate a wrapper for bundle:
rvm wrapper 1.9.3 ruby-1.9.3 bundle
and use the full path to the wrapper as displayed by this command:
which ruby-1.9.3_bundle
You told right, Eric, I do it by myself and run in development mode is fine.
This example can't be used properly, it is still very crude.
My config/unicorn_init file:
TIMEOUT=${TIMEOUT-60}
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="PATH=$_PATH GEM_HOME=$_GEM_HOME GEM_PATH=$_GEM_PATH $APP_ROOT/.bundle/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb"
set -e
action="$1"
set -u
old_pid="$PID.oldbin"
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $old_pid && kill -$1 `cat $old_pid`
}
case $action in
start)
sig 0 && echo >&2 "Already running" && exit 0
su -c "$CMD" - $APP_USER
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
su -c "$CMD" - $APP_USER
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $old_pid && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $old_pid
then
echo >&2 "$old_pid still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su -c "$CMD" - $APP_USER
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
echo "#\!/bin/bash\n_PATH=$PATH\n_GEM_HOME=$GEM_HOME\n_GEM_PATH=$GEM_PATH\nAPP_ROOT=$(pwd)\nAPP_USER=$USER\n$(cat config/unicorn_init)" > config/unicorn_init.sh
chmod +x config/unicorn_init.sh
This is an update to this question/answer from 6 years ago. As of RVM 1.29.4 and Ubuntu 16.04. The answer from mpapis is still valid for older versions of ubuntu and rvm.
As of RVM 1.29.4, the above answer vm wrapper 1.9.3 ruby-1.9.3 unicorn no longer works, nor is it neccessary anymore.
Wrappers are already created, and can be found in their location along with the proper gemset if needed.
Look in the following directory location /usr/local/rvm/wrappers. There you will find a link to your desired ruby version & gemset. Following that link will bring you to all of it's wrappers: unicorn, unicorn_rails, god, puma, thin, thor, ..., etc.
Example:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/var/rails/com.domain.site/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
DAEMON=/usr/local/rvm/wrappers/ruby-2.5.1#app/unicorn
CMD="$DAEMON -D -c $APP_ROOT/config/unicorn.rb -E production"
Alternatively you could also use the direct path as well:
DAEMON=/usr/local/rvm/gems/ruby-2.5.1#app/wrappers/unicorn
You get the idea (^_^)