当crontab尝试执行脚本时:
它为我创建了一个带有以下内容的日志文件:
这是我在unicorn_start.sh中的内容:
如果我手动执行
编辑:
@reboot sh /home/username/unicorn_start.sh > /home/username/unicorn_start.out 2>&1
它为我创建了一个带有以下内容的日志文件:
/usr/bin/env: ‘ruby’: No such file or directory
。这是我在unicorn_start.sh中的内容:
#!/bin/bash
PATH="$PATH:/usr/local/rvm/gems/ruby-1.9.2-p180/bin"
/etc/init.d/unicorn_myapp start
如果我手动执行
unicorn_myapp
或unicorn_start.sh
,一切都正常。据我了解,我需要将一个额外的路径添加到$PATH变量中。怎么做呢?提前感谢。编辑:
unicorn_init.sh:
#!/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-60}
APP_ROOT=/home/username/appname
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
AS_USER=root
set -u
OLD_PIN="$PID.oldbin"
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}
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
/etc/init.d/
目录下)吗?在这种情况下,你应该将其作为一个服务来启动,而不是使用cron
。请参考此链接:https://askubuntu.com/questions/19320/how-to-enable-or-disable-services - Melebiussudo service unicorn_appname start
的形式启动时,它给出一个错误:/etc/init.d/unicorn_appname: 1: eval: bundle: not found
。 - Alex Zakruzhetskyi/etc/init.d/unicorn_myapp
。到目前为止,你发布的代码中没有包含对/usr/bin/env
的调用。 - Melebiusunicorn_myapp
是一个符号链接,指向位于 Rails 项目的app/config
文件夹中的unicorn_init.sh
。如果你需要unicorn_init.sh
的内容,我会在帖子中添加它。 - Alex Zakruzhetskyi