我对bash脚本编写起来相当新,但我觉得我真的错过了一些基本的东西。我试图在Ubuntu 14.04机器上尝试几乎没有修改过的Mike Perham's upstart sidekiq script版本,但几乎没有按预期评估任何内容:
这是我修改后的脚本:
# /etc/init/sidekiq.conf - Sidekiq config
# This example config should work with Ubuntu 12.04+. It
# allows you to manage multiple Sidekiq instances with
# Upstart, Ubuntu's native service management tool.
# change to match your deployment user
setuid deploy
setgid deploy
stop on (stopping workers or runlevel [06])
respawn
respawn limit 3 30
instance $index
script
# this script runs in /bin/sh by default
# respawn as bash so we can source in rbenv
exec /bin/bash <<EOT
# use syslog for logging
# exec &> /dev/kmsg
# pull in system rbenv
export HOME=/home/deploy
echo "home is $HOME"
source /home/deploy/.bashrc
echo "path is $PATH"
cd /home/deploy/domain_freek/current
echo "user is $(whoami) and pwd is $(pwd) and rbenv is located at $(which rbenv)"
exec bundle exec sidekiq -i ${index} -e production
EOT
end script
这是我在upstart日志文件中得到的输出:
home is
path is /usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
user is deploy and pwd is / and rbenv is located at
/bin/bash: line 12: exec: bundle: not found
答案 0 :(得分:15)
2变化完全不同:
1)在exec /bin/bash << 'EOT'
中向EOT添加硬报价(相信Mat,谢谢!)
2)不使用source加载.bashrc,而是将.bashrc中的rbenv行直接添加到upstart脚本中。将source /home/deploy/.bashrc
替换为:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
我不知道为什么这两个变化有所不同,如果这与ubuntu,upstart或bash的新版本相关。如果有人可以解释,请加入。
我已经为所有寻找答案的人提供了完整的工作脚本:
# /etc/init/sidekiq.conf - Sidekiq config
# This example config should work with Ubuntu 12.04+. It
# allows you to manage multiple Sidekiq instances with
# Upstart, Ubuntu's native service management tool.
#
# See workers.conf for how to manage all Sidekiq instances at once.
#
# Save this config as /etc/init/sidekiq.conf then mange sidekiq with:
# sudo start sidekiq index=0
# sudo stop sidekiq index=0
# sudo status sidekiq index=0
#
# or use the service command:
# sudo service sidekiq {start,stop,restart,status}
#
description "Sidekiq Background Worker"
# no "start on", we don't want to automatically start
stop on (stopping workers or runlevel [06])
# change to match your deployment user
setuid deploy
setgid deploy
respawn
respawn limit 3 30
# TERM is sent by sidekiqctl when stopping sidekiq. Without declaring these as normal exit codes, it just respawns.
normal exit 0 TERM
instance $index
script
# this script runs in /bin/sh by default
# respawn as bash so we can source in rbenv
exec /bin/bash << 'EOT'
# use syslog for logging
# exec &> /dev/kmsg
# pull in system rbenv
export HOME=/home/deploy
echo "$HOME"
#source /home/deploy/.bashrc
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
echo "$PATH"
cd /home/deploy/domain_freek/current
echo "user is $(whoami) and pwd is $(pwd) and rbenv is located at $(which rbenv)"
exec bundle exec sidekiq -i ${index} -e production
EOT
end script
答案 1 :(得分:3)
如果shell以非交互模式运行,Ubuntu 14.04上的默认.bashrc
文件会立即返回几行。当您从bashrc中删除这些行时,source
将在upstart中按预期工作。
〜/ .bashrc (要删除的行)
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
答案 2 :(得分:0)
Josh的解决方案在使用rbenv的ubuntu 14.04上对我不起作用。然而,这确实有效:
exec /bin/bash <<EOF
export RBENV_ROOT=/home/ubuntu/.rbenv
export RBENV_VERSION=2.2.2
cd /var/www/app/current
exec /home/ubuntu/.rbenv/bin/rbenv exec bundle exec sidekiq -i ${index} -e production
EOF