init.d脚本不会运行执行命令,但是当在终端

时间:2015-11-19 16:46:17

标签: bash terminal command init.d

我有以下命令,我的init.d独角兽脚本运行。 此命令在终端中手动没有问题,但拒绝 在我的init.d / unicorn文件中工作

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 )

这是init.d文件

#!/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

1 个答案:

答案 0 :(得分:1)

将CMD变量抽象为函数可以解决问题。 正如tripleee的资源共享所暗示的那样。

#!/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