gpt4 book ai didi

bash - Ubuntu 14.04 上的 upstart 或 bash 脚本是否发生了变化? (尝试用 upstart 启动 sidekiq)

转载 作者:行者123 更新时间:2023-11-29 09:01:33 24 4
gpt4 key购买 nike

我对 bash 脚本编写相当陌生,但感觉我真的缺少一些基本的东西。我正在尝试 Mike Perham's upstart sidekiq script 的几乎没有修改的版本在 Ubuntu 14.04 机器上,几乎没有任何东西按预期进行评估:

  • 导出似乎不起作用
  • source 似乎没有评估我在 .bashrc 中更改的 PATH 变量或运行 rbenv初始化命令
  • cd 似乎不会更改目录,除非 $(pwd) 命令不是评估它的正确方法

这是我修改后的脚本:

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

最佳答案

2 改变让一切变得不同:

1) 在 exec /bin/bash << 'EOT' 中为 EOT 添加硬引号(归功于 Mat,谢谢!)

2) 不使用源加载 .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

关于bash - Ubuntu 14.04 上的 upstart 或 bash 脚本是否发生了变化? (尝试用 upstart 启动 sidekiq),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26439555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com