在Heroku上以编程方式运行bash脚本文件。

huangapple go评论92阅读模式
英文:

Run bash script file in Heroku programatically

问题

我有一个用Go编写并部署在Heroku上的Web应用程序。最近,我实现了一个新功能,需要运行一个bash脚本文件并获取其输出。我正在使用的命令是:

out, err := exec.Command("bash", script_path, arg).Output()

其中script_path是脚本的绝对路径(通过使用调用者的文件路径动态构建),arg是脚本的输入。

这个命令在我的本地机器上运行正常,但在Heroku上不行:

> 18:51:40 http: panic serving 10.238.8.204:17763: exit status 127

附注:如果我在"heroku run bash"上运行相同的命令,它可以正常工作。

有什么想法吗?

英文:

I have a web app written in Go and deployed on Heroku. Recently, I've implemented a new feature that needs to run a bash script file and retrieve its output. The command I'm using is:

out, err := exec.Command("bash", script_path, arg).Output()

Where script_path is the absolute path to the script. (which is built dynamically by using the caller filepath) And arg is an input to the script.

This command works fine in my local machine but not in Heroku:

> 18:51:40 http: panic serving 10.238.8.204:17763: exit status 127

ps: If I run the same command on "heroku run bash", it works.

Any thoughts?

答案1

得分: 1

一个返回代码为127的shell是用来表示“命令未找到”。

你可以通过以下示例来观察到这种行为。

$ nonesuch
-bash: nonesuch: command not found
$ echo $?
127

这种问题通常意味着你需要将

 /full/path/to/myscriptdir

添加到PATH环境变量中,即

 export PATH="$PATH:/full/path/to/myscriptdir withSpaces"

可以在几个地方完成这个操作,但具体取决于你的组织对此类事情的规定。

要立即解决这个问题,可以将其添加到运行Heroku的用户ID的$HOME/.profile文件中,或者可能有一个专门的Heroku配置文件可以添加到其中(我对Heroku没有经验)。

有趣的是,这篇文章Heroku配置文件没有提到PATH变量。


正如你发现的那样,你可以直接编辑你的代码,包括完整的路径,例如:

out, err := exec.Command("/bin/bash", path.Join(".", "src", "ext", "dextenso.sh"), 

希望对你有帮助。

英文:

a shell return code of 127 is to designate "Command not found".

You can see this behavior with this small example.

$ nonesuch
-bash: nonesuch: command not found
$ echo $?
127

This sort of issue usually means you need to add

 /full/path/to/myscriptdir

to the PATH env var. i.e.

 export PATH="$PATH:/full/path/to/myscriptdir withSpaces" 

There are several places this can be accomplished, but it depends on your organizations rules for such things.

To immediately resolve the problem, add to the $HOME/.profile file of the userID that runs Heroku OR there is likely a Heroku specific config file that it could be added to. (I don't have experience w Heroku).

It's interesting that this article Heroku config files doesn't mention PATH variable.


As you have discovered, you can edit your code to include the full path directly, i.e.

out, err := exec.Command("/bin/bash", path.Join(".", "src", "ext", "dextenso.sh"), 

IHTH

huangapple
  • 本文由 发表于 2015年12月1日 02:57:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/34005895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定