在终端我写道:ruby lib/scripts/script.rb
在script.rb上我有两个脚本......
script.rb
require 'config/environment'
#first script
#notifier user that question deadline is over and show statistics
inquiry.each do |i|
question = i.question
#respondent = Respondent.find(:all, :conditions => ["id = (?)", i.respondent_id])
respondent = i.respondent
#Notifier.deliver_deadline_notification(inquiry, user, question, respondent)
Notifier.deliver_statistics_notification(inquiry, question, user, respondent)
#respondent = Respondent.find(:all, :conditions => ["id = (?)", i.respondent_id])
#respondents.each do |r|
#end
end
#second script
respondents = Respondent.find(:all)
inquiries = Inquiry.find(:all, :conditions => ["is_answered = 0 AND respondent_id = (?)", respondents])
#respondents = Respondent.find(:first, :conditions => ["id = (?)", inquiries])
questions = Question.find(:all)
qdead = questions.deadline
dead_line_date = qdead - 1.days - 0.minutes - 0.seconds
get_time_now = Time.now.strftime('%m/%d/%Y')
我在一个rb文件中有这两个脚本(1个脚本执行某些操作,第二个是另一个)。我的问题是......如何在控制台中编写同时启动第一个脚本和第二个脚本?我知道我用了一些AGVG?但是怎么样?
非常感谢!
UPD: 使用cron我写道:
0 0 * * * /usr/bin/rails-run-script myproject script oncoming
0 1 * * * /usr/bin/rails-run-script myproject script missed
d
欢迎和错过 - 争论。我如何在我的script.rb中弃用它们。脚本如何知道我使用错过或接近?
答案 0 :(得分:1)
如果我的问题是正确的,那么你所拥有的是一个基于你传递给脚本的命令行参数的行为不同的脚本,对吗?
因此script missed
执行a script oncoming
执行其他操作。
如果它真的那么简单,你可以简单地决定ARGV.first。
case ARGV.first
when "oncoming"
# your "oncoming" logic (preferably wrapped in a method call) here
when "missed"
# your "missed" logic
else
$stderr.puts "Call the script with either missed or oncoming"
exit 1
end
但是,如果你想做任何稍微复杂的选项,我强烈建议使用rubys stdlib中包含的optparse.rb(http://ruby-doc.org/stdlib/libdoc/optparse/rdoc /index.html)