简体   繁体   English

用Cron运行Ruby的问题

[英]Problem with running Ruby with Cron

My ruby file is like this. 我的红宝石文件就是这样。

`mkdir #{HOST} -p`

It works fine by: ruby mycode.rb 它可以正常工作: ruby mycode.rb

But in a cron job 但是,在一个cron工作中

0 * * * * ruby ~/backup.rb >> backup.log 0 * * * *红宝石〜/ backup.rb >> backup.log

It will a -p folder. 它将是-p文件夹。 Why? 为什么?

The #1 problem that anybody runs into with cron jobs is that usually, for security reasons, cron jobs run with a minimal $PATH . 任何人都使用cron作业遇到的#1问题是,出于安全原因,通常cron作业以最小的$PATH运行。 So, it could be that your cron job runs with a different path than when you run the script from the shell, which would mean that it is possible that within the cron job a different mkdir comman gets called, which interprets its arguments differently. 因此, 可能是您的cron作业运行的路径与从shell运行脚本时的路径不同,这意味着在cron作业中可能会调用不同的mkdir逗号,这将以不同的方式解释其参数。

Usually , the first filename argument stops option processing and everything that comes after that will be treated as a filename. 通常 ,第一个filename参数停止选项处理,此后出现的所有内容都将被视为文件名。 So, since #{HOST} is a filename, everything after that will also be treated as a filename, which means that the call will be interpreted as "make two directories, one named #{HOST} and the other named -p " If you look for example at the specification of mkdir , it is simply illegal to pass an option after the filenames. 因此,由于#{HOST}是文件名,此后的所有内容将被视为文件名,这意味着该调用将被解释为“建立两个目录,一个名为#{HOST} ,另一个名为-p ”。例如,您查看mkdir的规范,在文件名传递选项完全是非法的。

Another possibility is that for some reason #{HOST} will be empty when running under cron. 另一种可能性是由于某种原因,在cron下运行时, #{HOST}将为空。 Then the whole call expands to mkdir -p , which again, depending on your implementation of mkdir might be interpreted as "create one directory named -p ". 然后,整个通话扩展到mkdir -p ,而这又取决于你的实现mkdir 可能被解释为“创建一个目录中名为-p ”。

It is not quite clear to me why you are passing the options and operands in the wrong order, instead of mkdir -p #{HOST} . 我不太清楚为什么您以错误的顺序传递选项和操作数,而不是mkdir -p #{HOST} It's also not clear to me why you use the shell at all , instead of just FileUtils.mkdir_p(HOST) . 这也是我不明白为什么你使用的shell 可言 ,而不是仅仅FileUtils.mkdir_p(HOST)

Another problem I've seen is the #! 我看到的另一个问题是#! script line fails when /usr/bin/env is used. 使用/ usr / bin / env时,脚本行失败。 For instance: 例如:

#!/usr/bin/env ruby

doesn't find ruby when running under cron. 在cron下运行时找不到红宝石。 You have to use 你必须用

#!/usr/local/bin/ruby

or the equivalent on your platform. 或您平台上的同等产品。

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

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