仅使用cron:/ usr / bin / env:ruby_executable_hooks:没有这样的文件或目录

时间:2015-07-17 18:10:45

标签: ruby perl cron

我有一个perl脚本,它使用system()来调用shell命令,该命令使用ruby_executable_hooks。

当我从命令行运行此脚本时,它可以完美地执行。

然而,当它从一个cron作业运行时,我得到两个错误之一: A)如果我不chdir()即。工作目录是/ home / foobar

// Defining my own ways of referring to the namespaces, regardless of prefix used
define('XMLNS_HP_XDM_1_1', 'http://www.hp.com/schemas/imaging/con/xdm/1.1/');
define('XMLNS_HP_DICT_1_0', 'http://www.hp.com/schemas/imaging/con/dictionaries/1.0/');

// Load the XML in the normal way
$sx = simplexml_load_string($xml);

// Switch to the first namespace, and look at the first Component
$component = $sx->children(XMLNS_HP_XDM_1_1)->Information->Component;

// Attributes with no : in are technically in no namespace at all,
// so we have to switch back to the null namespace using attributes()
$component_id = (string)$component->attributes(null)->id;

// Switch to the second namespace to find the MakeAndModel
$make_and_model = (string)$component->children(XMLNS_HP_DICT_1_0)->MakeAndModel;

echo "$component_id : $make_and_model";

B)如果我是chdir(“[doc root] / cgi-bin”)

/usr/bin/env: ruby_executable_hooks: No such file or directory

从cron运行时无法找到某些文件的原因可能是什么?

3 个答案:

答案 0 :(得分:4)

我在这里找到答案: https://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables

具体来说,我只是把#34;。 $ HOME / .profile中;"在cron命令面前如此:

0 5 * * *。 $ HOME / .profile文件; /路径/到/命令/到/运行

谢谢大家让我走上正轨。

答案 1 :(得分:0)

几乎可以肯定,您的环境变量存在问题。当您从cron运行某些内容时,您的.bashrc未被激活,因此可能设置的环境变量,例如您指向的ruby版本以及您的PATH看起来可能不同。

如何解决这个问题取决于您的环境以及您可能使用哪些工具来选择您的ruby版本(rvm,chr​​uby,rbenv等等)。

您可能希望使用类似which ruby的内容来确定要执行哪些ruby并在crontab中使用它。

答案 2 :(得分:0)

我不太确定为什么可以这样做,但是如果有人在MacOS上使用RVM,那么这对我有用。我正在使用名为backup的gem运行数据库备份。我必须将宝石放入应用程序的Gemfile中。不确定为什么我必须CD到应用程序目录。

* * * * * source /Users/john/.rvm/environments/ruby-2.7.1; cd /path/to/app && bundle exec backup perform -t mybackup