Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to get a unicorn server to start when the server boots. I've created a shell script which works if I log as the ubuntu user and run

/etc/init.d/unicorn start

Shell script

#!/bin/sh

case "$1" in
start)
    cd /home/ubuntu/projects/asbest/current/
    unicorn_rails -c /home/ubuntu/projects/asbest/current/config/unicorn.rb -D -E production

;;
stop)
    if ps aux | awk '{print $2 }' | grep `cat ~/projects/asbest/current/tmp/pids/unicorn.pid`> /dev/null; then kill `cat ~/projects/asbest/current/tmp/pids/uni$
;;
restart)
    $0 stop
    $0 start
;;
esac

When I rebooted the server I noticed that the unicorn server wasn't listening to a socket. Since I ran the code succesfully as the ubuntu user I modified the script to let it always use the ubuntu user via sudo.

#!/bin/sh

case "$1" in
start)
    cd /home/ubuntu/projects/asbest/current/
    sudo -u ubuntu unicorn_rails -c /home/ubuntu/projects/asbest/current/config/unicorn.rb -D -E production
;;
stop)
    if ps aux | awk '{print $2 }' | grep `cat ~/projects/asbest/current/tmp/pids/unicorn.pid`> /dev/null; then sudo -u ubuntu kill `cat ~/projects/asbest/current/tmp/pids/uni$
;;
restart)
    $0 stop
    $0 start
;;
esac

After rebooting unicorn still wouldn't start, so I tried running the script from the command line. Now I get the following error

sudo: unicorn_rails: command not found

I've searched high and low to what could cause this, but I'm afraid I've tapped my limited understanding of Linux. From what I can understand is that although sudo should use the ubuntu user to execute the commands, it still uses the environment of the root user, which isn't configured to run ruby or unicorn. Does anybody have any experience with this?

share|improve this question

migrated from stackoverflow.com Oct 18 '12 at 16:57

This question came from our site for professional and enthusiast programmers.

add comment

3 Answers

Use global vars UNICORN_* like this:

UNICORN_HOME=/the/path
UNICORN_RAIL=${UNICORN_HOME}/unicorn_rail
UNICORN_CONFIG=${UNICORN_HOME}/config/unicorn.rb
UNICORN_PID=${UNICORN_HOME}/tmp/pids/unicorn.pid
UNICORN_USER=ubuntu

sudo -u ${UNICORN_USER} ${UNICORN_RAIL} -c $UNICORN_CONFIG -D -E production

Another good way is to extract global vars in /etc/default/unicorn:

UNICORN_HOME=/the/path
UNICORN_RAIL=${UNICORN_HOME}/unicorn_rail
UNICORN_CONFIG=${UNICORN_HOME}/config/unicorn.rb
UNICORN_PID=${UNICORN_HOME}/tmp/pids/unicorn.pid
UNICORN_USER=ubuntu

and in your init script add change all vars by:

if [ -f /etc/default/unicorn ]; then
    . /etc/default/unicorn
fi
share|improve this answer
add comment

Instead of using sudo, try using su [username] and then execute the command.

share|improve this answer
add comment

You need to specify the path to unicorn_rails:

UNICORN_HOME=/home/ubuntu/projects/asbest/current
cd $UNICORN_HOME
sudo -u ubuntu $UNICORN_HOME/unicorn_rails -c $UNICORN_HOME/config/unicorn.rb -D -E production

Have you configured sudo appropriately?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.