init.d脚本不会运行执行命令,但命令在手工运行时终止
问题描述:
我有以下命令,我的init.d独角兽脚本运行。 该命令没有问题的作品在终端手动,但拒绝 工作在我的init.d /麒麟文件init.d脚本不会运行执行命令,但命令在手工运行时终止
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
答
抽象的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
+1
我不明白为什么你想要或需要在子shell中运行'env'。您可以删除'cd'后面的圆括号。 – tripleee
“拒绝工作”是指什么? –
你正在把你的命令放在'CMD'中,但是你永远不会使用它。无论如何,你可能不应该使用变量;请参阅http://mywiki.wooledge.org/BashFAQ/050 – tripleee