[英]What's wrong with this bash script (to be used by Foreman)
I need an additional process to be run in development called Proxylocal (this allows external web services to hit http endpoints on my local machine). 我需要在开发中运行一个名为Proxylocal的附加过程(这使外部Web服务可以访问本地计算机上的http端点)。
In order to do this reliably though I would like to add the process into my Procfile. 为了可靠地执行此操作,尽管我想将该过程添加到Procfile中。 Since the process should not be run in production I am using this technique to do the switching.
由于该过程不应在生产中运行,因此我正在使用此技术进行切换。
My Procfile looks like this: 我的Procfile看起来像这样:
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
redis: redis-server
worker: bundle exec sidekiq
proxylocal: bin/proxylocal
and bin/proxylocal
looks like this: 和
bin/proxylocal
看起来像这样:
#!/bin/bash
if [ "$RACK_ENV" == "development" ]
then
proxylocal 5000 --host mypersonalmachine
fi
(I've never done any bash scripting so have no idea whether this is the correct syntax - I just copied it). (我从未做过任何bash脚本编写,因此不知道这是否是正确的语法-我只是复制了它)。
However each time I run foreman start
it immediately exits with only the following error: 但是,每次我运行
foreman start
它都会立即退出,并且仅出现以下错误:
10:57:25 proxylocal.1 | started with pid 14935
10:57:25 proxylocal.1 | exited with code 0
10:57:25 system | sending SIGTERM to all processes
If I remove the if
statement and change bin/proxylocal
to simply: 如果删除
if
语句,然后将bin/proxylocal
更改为:
proxylocal 5000 --host mypersonalmachine
Then that does work successfully. 这样就可以成功工作。 Why then does the
if
statement make everything baulk? 那么,为什么
if
语句会使所有内容无所适从?
The following code does work as long as $RACK_ENV is equal to "development". 只要$ RACK_ENV等于“开发”,以下代码就可以工作。 I had to remove the double quotes around
$RACK_ENV
and to change the ==
to a single =
. 我必须删除
$RACK_ENV
周围的双引号,并将==
更改为单个=
。
if [ $RACK_ENV = "development" ]; then
proxylocal 5000 --host brojure
fi
However... it only works as long as $RACK_ENV
does equate to 'development'. 但是...仅在
$RACK_ENV
等同于'development'时,它才起作用。 When it doesn't it chokes. 如果没有,它会窒息。
A friend suggested that the issue may be that the Procfile is expecting a process ID to be returned from the script. 一位朋友建议说,问题可能出在Procfile期望从脚本返回进程ID。 So in the instance when we're not on development it's got nothing to process.
因此,在我们不进行开发的情况下,无需进行任何处理。
Is there a way in which I could spin up a zero-overhead process just to be able to return an ID. 有没有一种方法可以加快零开销的过程,以便能够返回ID。 It's ugly but probably not too costly...
丑陋但可能不太昂贵...
The problem is that when RACK_ENV is not "development" there is no process run so Foreman assumes that the script has crashed once it exits. 问题是,当RACK_ENV不是“ development”时,没有进程在运行,因此Foreman认为脚本一旦退出便已崩溃。
The right thing to do here is to create a "web" wrapper script that runs the proxy in development. 正确的做法是创建一个在开发中运行代理的“ Web”包装脚本。 Something like this:
像这样:
#!/usr/bin/env bash
cleanup() {
kill $proxy_pid
}
if [ "$RACK_ENV" == "development" ]; then
proxylocal 5000 --host $PROXY_HOST &
proxy_pid=$(cat thepidfile)
trap cleanup EXIT
fi
bundle exec unicorn -p $PORT -c ./config/unicorn.rb
Please note that the answer from David Dollar should be the correct one as he wrote Foreman. 请注意,大卫·杜尔(David Dollar)的答案应该是他写给工头的正确答案。 However I still had problems getting it to work and so (for better or worse) this was what did eventually work for me:
但是,我仍然无法使它正常工作,所以(不管是好是坏)这就是最终对我有用的东西:
# Procfile: #Procfile:
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
redis: redis-server
worker: bundle exec sidekiq
proxylocal: bin/proxylocal_local
# bin/proxylocal_local #bin / proxylocal_local
#!/usr/bin/env bash
if [ $RACK_ENV = "development" ]; then
proxylocal 5000 --host $PROXYLOCAL_HOST
else
cat
fi
# .env #.env
PROXYLOCAL_HOST=hostname_you_want_to_use
Also note that you'll need to chmod the proxylocal_local file so that it is executable. 还要注意,您需要chmod proxylocal_local文件以便可执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.