简体   繁体   English

脚本可从Shell运行,但不能从Ruby运行

[英]Script works from shell but not Ruby

I have a script that fills in parameters for multiple spark-submit jobs. 我有一个脚本,可以填写多个火花提交作业的参数。 The jobs work when I copy and paste them into shell, but they fail when I put the string in backticks and execute it from within Ruby. 当我将它们复制并粘贴到shell中时,这些工作就可以工作,但是当我将字符串放入反引号并在Ruby中执行时,它们就会失败。

spark-submit --master yarn-cluster \
  --num-executors 2 \
  --files ... \
  --class ... \
  -otherflags ...

Ruby returns Ruby返回

sh: 1: spark-submit: not found

even though spark-submit is in the system path. 即使spark-submit在系统路径中。 Even more confusing is that: 更令人困惑的是:

`spark-submit` 

within Ruby seems to call the shell command correctly. 在Ruby中似乎可以正确调用shell命令。

Consider what's different when you are at the command-line in the shell, versus when code is running in Ruby. 考虑一下在shell中的命令行时和在Ruby中运行代码时的不同之处。

The shell has PATH defined, which it walks looking for the command, hopefully eventually finding the desired one. 外壳程序定义了PATH ,它在其中查找命令,最终希望找到所需的PATH

Ruby doesn't do that, and instead, you need to define the absolute path to the executable. Ruby不会这样做,而是需要定义可执行文件的绝对路径。 There are various ways of doing it but the simplest is to use 这样做的方法有很多种,但最简单的方法是使用

where spark-submit

at the command-line, then copy/paste that into your Ruby script. 在命令行中,然后将其复制/粘贴到您的Ruby脚本中。 Most likely you'll end up with something like: 最有可能的结果是:

/usr/local/bin/spark-submit

or 要么

/path/to/rubys/gems/.../bin/spark-submit

spark-submit

within Ruby seems to call the shell command correctly. 在Ruby中似乎可以正确调用shell命令。

Ruby is trying to find the command in the current directory. Ruby尝试在当前目录中查找命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM