0

I have the following command that my init.d unicorn script runs. This commands works with no issue manually in terminal but refuses to work in my init.d/unicorn file

cd /var/www/myapp/current && ( RAILS_ENV=production BUNDLE_GEMFILE=/var/www/myapp/current/Gemfile /usr/bin/env bundle exec unicorn -c /var/www/myapp/current/config/unicorn/production.rb -E deployment -D )

Here is the init.d file

#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: postgresql nginx
# Required-Stop: 
# Should-Start: 
# Should-Stop: 
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop unicorn
# Description: UNICORN
### END INIT INFO
set -e
APP_ROOT=/var/www/myapp/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
RAILS_ENV=production
BUNDLE_GEMFILE=$APP_ROOT/Gemfile
CMD="cd $APP_ROOT && ( RAILS_ENV=$RAILS_ENV BUNDLE_GEMFILE=$APP_ROOT/Gemfile /usr/bin/env bundle exec unicorn -c $APP_ROOT/config/unicorn/$RAILS_ENV.rb -E deployment -D )"

action="$1"
set -u

cd $APP_ROOT || exit 1

sig () {
        test -s "$PID" && kill -$1 `cat $PID`
}

case $action in
start)
        sig 0 && echo >&2 "Already running" && exit 0
        $CMD
        ;;
stop)
        sig QUIT && exit 0
        echo >&2 "Not running"
        ;;
esac
2
  • What does "refuses to work" mean exactly? Commented Nov 19, 2015 at 16:51
  • 1
    You are putting your commands in CMD but then you never use it. You should probably not be using a variable here anyhow; see mywiki.wooledge.org/BashFAQ/050 Commented Nov 19, 2015 at 17:13

1 Answer 1

1

Abstracting the CMD variable to a function fixed the issue. As indirectly suggested by tripleee's resource shared.

#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: postgresql nginx
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop unicorn
# Description: UNICORN
### END INIT INFO
set -e
APP_ROOT=/var/www/myapp/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
RAILS_ENV=production
BUNDLE_GEMFILE=$APP_ROOT/Gemfile
action="$1"
set -u

cd $APP_ROOT || exit 1

run(){
        cd $APP_ROOT && ( RAILS_ENV=$RAILS_ENV BUNDLE_GEMFILE=$APP_ROOT/Gemfile /usr/bin/env bundle exec unicorn -c $APP_ROOT/config/unicorn/$RAILS_ENV.rb -E deployment -D )
}

sig () {
        test -s "$PID" && kill -$1 `cat $PID`
}

case $action in
start)
        sig 0 && echo >&2 "Already running" && exit 0
        run
        ;;
stop)
        sig QUIT && exit 0
        echo >&2 "Not running"
        ;;
esac
Sign up to request clarification or add additional context in comments.

1 Comment

I don't see why you would want or need to run env in a subshell. You can probably remove the parentheses after the cd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.