从crontab运行从Ruby脚本运行系统调用的错误32512

时间:2011-06-24 12:02:20

标签: ruby linux cron

从用户的crontab运行时,我遇到了使Ruby脚本正常工作的问题。当您从终端以该用户身份运行它时,它可以正常工作。从crontab运行时,系统调用失败。

这是脚本;

#!/usr/bin/ruby

require "net/http"
require "logger"
require "pp"

begin
        Dir.chdir("wca")
        time1 = Time.new
        filename = "updated" + "_" + time1.hour.to_s + "_" + time1.min.to_s + "_" + time1.sec.to_s + ".log"
        log = Logger.new(filename) 
        log.debug "===Checking For New Build"
        source = Net::HTTP.get('yr-qa-svr2', '/Wave/index.html')
        myFile = File.new("data/old.html","rb")
        old = myFile.read
        myFile.close

        if old != source then
            log.debug "   Server Version Changed, running tests"
            status = system("runWatir","webdriver.rb")
            if status then
                log.debug "   Command run correctly"
                myFile = File.new("data/old.html","w")
                myFile.puts source
                myFile.close
            else
                log.debug "   Failed to run command"
                log.debug "   Error number " + $?.to_s
            end
        else
            log.debug "   No Updates to Server Version"
        end
rescue Exception => e  
      print "Exception occured: " + e  + "\n"
      print e.backtrace
end 

从crontab运行时会生成此日志;

    # Logfile created on Fri Jun 24 02:00:01 +0100 2011 by logger.rb/22285
    D, [2011-06-24T02:00:01.333921 #4409] DEBUG -- : ===Checking For New Build
    D, [2011-06-24T02:00:01.433632 #4409] DEBUG -- :    Server Version Changed, running tests
    D, [2011-06-24T02:00:01.462700 #4409] DEBUG -- :    Failed to run command
    D, [2011-06-24T02:00:01.462919 #4409] DEBUG -- :    Error number 32512

命令runWatir是位于wca目录中的bash shell脚本。

#!/bin/bash
rm logs/*.log
rm logs/*.png
export DATE=`date`
ruby -W0 $1|tee logs/$1.log
export RESULT=`grep assertions logs/$1.log`
export TIMED=`grep Finished logs/$1.log`
export BUILD=`cat logs/build.log`
ruby library/GenEmailMsg.rb "WATIR Results for $DATE" "Attached are the results from the WATIR automated test [$1] run with $BUILD..The results are $RESULT..$TIMED" logs/$1.log

我的菜鸟错误是什么?

1 个答案:

答案 0 :(得分:3)

cron作业以不同的用户身份运行,具有不同的环境变量集。除非您手动设置,否则您的PATHrvm设置等将无法使用。我猜你被其中一个击中了。要尝试的是确保您的ruby版本在cron作业和常规shell中相同,用户是相同的,并且路径是相同的。